1

I'm trying to insert data MondoDB with AngularJS's $http service , but one of the variables and the array What is in the collection , What to do ?

nome: string. autor: string. genero: array. info: string.

Collection: mangas.

db.mangas.insert({nome: 'toriko', autor:'test', genero:['Ação','Aventura'], info:'...'})

Server.Js, Find mangas.

app.get('/mangas', function(req, res){
  console.log('i receive a get request')
  db.mangas.find(function (err, mangas){
    console.log(mangas);
    res.json(mangas);
  });

});

Server.Js, Insert mangas.

app.post('/mangas', function (req, res) {
  console.log(req.body);
  db.mangas.insert(req.body, function(err, doc) {
    res.json(doc);
  });
});

index.html, ng-click="addManga"

<tr>
  <td><input class="form-control"manga.nome></td>
  <td><input class="form-control"manga.autor></td>
  <td><input class="form-control"manga.genero></td>
  <td><input class="form-control"manga.info></td>
  <td><button class="btn btn-primary" ng-click="addManga()">Add manga</button></td> ----adiciona o metodo que se encontra no controller
</tr>

     <tr ng-repeat="manga in mangas">
       <td>{{manga.nome}}</td>
       <td>{{manga.autor}}</td>
       <td>{{manga.genero}}</td>
       <td>{{manga.info}}</td>
     </tr>

Controller.js

$http.get('/mangas').success(function(response) {
    console.log("eu recevi a data requisitada");
    $scope.mangas = response;
  });

  $scope.addManga = function() {
    console.log($scope.mangas);
    $http.post('/mangas', $scope.mangas).success(function(response) {
      console.log(response);

    })};
3
  • Your question is unclear. What is your problem? Commented Feb 1, 2016 at 12:59
  • sorry, i cant insert values ​​with $scope.addManga Commented Feb 1, 2016 at 13:03
  • On server.js , db.mangas.find, work, but no with db.mangas.insert, Commented Feb 1, 2016 at 13:05

1 Answer 1

1

In HTML:

you should use ng-model in html to bind with controller

Like:

<td><input class="form-control" ng-model="manga.nome"></td>

instead of

<td><input class="form-control"manga.nome></td>

and in controller:

you should use $scope.manga instead of $scope.mangas because of you bind manga in html input fields.

$scope.manga = {};
$scope.addManga = function() {
    console.log($scope.manga);
    $http.post('/mangas', $scope.manga).success(function(response) {
      console.log(response);

})};
Sign up to request clarification or add additional context in comments.

5 Comments

Can I Take A doubt ? gender is array, the code sends string , how to improve it?
I want to create a manga reader , it would be Necessary to use mongoose to define the layout, to create a good website ? I have many doubts,
why gender is array? it should be string but multiple gender need to shown in UI not in DB . Not necessary to use mongoose but if you use mongoose then you can manage easily and code will be readable and better for understanding.
I meant, gender, because it will have categories for example: action, adventure, so will facilitate the search by category, gender is genero in português.
well, one soln can be, add another button and function to push gender. like: $scope.addGender = function(){ $scope.genders.push($scope.gender);}; and in $scope.addManga function assign those genders $scope.manga.genders = $scope.genders;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.