0

I'm using NodeJS, AngularJS, and MongoDB with mongoose to make a website. I'm having some trouble for modify an object in mongoDB. I follow a tutorial and everything work with the exemple. But I try with another table, the name of the new table is post and I have a problem when I want to change a value in the table.

Here is my Model :

var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var Todo = require('../models/Post.js');

    var PostSchema = new mongoose.Schema({
        nomReseau : String,
        corps : String,
        etat : String,
        section : String
    });
var Post = mongoose.model('PostReseaux', PostSchema);
module.exports = Post;

Here you can see the html :

 <ul>
    <li ng-repeat="post in posts ">
        <div class="card blue-grey darken-1">
            <div class="card-content white-text">
                <span class="card-title">Ticket</span>
                <p>
                    <a ng-show="!editing[$index]" href="#/{{post._id}}">{{post.corps}}</a>
                </p>
                <input type="checkbox" ng-model="post.etat" ng-change="update($index)">
                <button ng-show="!editing[$index]" ng-click="edit($index)">Editer</button>
                <button ng-show="!editing[$index]" ng-click="remove($index)">Supprimer</button>
                <button ng-show="!editing[$index]" ng-click="passer($index)">Passer</button>

                <input ng-show="editing[$index]" type="text" ng-model="post.corps">
                <button ng-show="editing[$index]" ng-click="update($index)">Confirmer</button>
                <button ng-show="editing[$index]" ng-click="cancel($index)">Annuler</button>
            </div>
        </div>
    </li>
</ul>
<a class="waves-effect waves-light btn" ng-click="save()">Créer ticket</a>

It's a simple ng-repeat who show every post in my database, if I use the remove or the save functions everything works I can add or remove a post in the db.

Here is the controller :

angular.module('app').controller('Feedback', ['$scope','$location','Posts', function($scope, $location, Posts) {
    $scope.title = 'Feedbacks';
    $(".button-collapse").sideNav();
    $scope.editing = [];
    $scope.posts= Posts.query();

    $scope.save = function(){
        if($scope.CorpsTicket.length < 1) return;
        var post = new Posts({ nomReseau: "Google+", corps : "test", etat : "aTraiter", section :"feedback" });

        post.$save(function(){
            $scope.posts.push(post);
        });
    }

    $scope.update = function(index){
        var post = $scope.posts[index].etat;
        Posts.update({id: post._id}, post);
        $scope.editing[index] = false;
    }

    $scope.passer = function(index){
        var post = $scope.posts[index];
        post.etat = "enCours";
        Posts.update({id: post._id}, post);
        $scope.editing[index] = false;
    }

    $scope.edit = function(index){
        $scope.editing[index] = angular.copy($scope.posts[index]);
    }

    $scope.cancel = function(index){
        $scope.posts[index] = angular.copy($scope.editing[index]);
        $scope.editing[index] = false;
    }

    $scope.remove = function(index){
        var post = $scope.posts[index];
        Posts.remove({id: post._id}, function(){
            $scope.posts.splice(index, 1);
        });
    }


}])

And here is the js for the backend :

var express = require('express');
var router = express.Router();

var mongoose = require('mongoose');
var Post = require('../models/Post.js');

/* GET /post listing. */
router.get('/', function(req, res, next) {
    Post.find(function (err, posts) {
        if (err) return next(err);
        res.json(posts);
    });
});


/* POST /post */
router.post('/', function(req, res, next) {
    Post.create(req.body, function (err, post) {
        if (err) return next(err);
        res.json(post);
});
});

/* GET /todos/id */
router.get('/:id', function(req, res, next) {
    Post.findById(req.params.id, function (err, post) {
        if (err) return next(err);
        res.json(post);
    });
});


/* PUT /todos/:id */
router.put('/:id', function(req, res, next) {
    Post.findByIdAndUpdate(req.params.id, req.body, function (err, post) {
        if (err) return next(err);
        res.json(post);
    });
});

/* DELETE /todos/:id */
router.delete('/:id', function(req, res, next) {
    Post.findByIdAndRemove(req.params.id, req.body, function (err, post) {
        if (err) return next(err);
        res.json(post);
    });
});

module.exports = router;

But if I click on edit($index) and I change the value of post.corps and I click on Confirmer the value change in the screen but not change in the database.

I think I have a problem with the update function, but I don't know what it is. Like is say, I have done a tutorial and, with the table create in the tutorial, everything works I can modify a value, but I got this bug with this new Table post.

Thanks

5
  • Please provide a Minimal, Complete, and Verifiable example. Commented Dec 9, 2015 at 14:43
  • I'm not familiar with Angular, but it looks like you are querying an array as if it were an object hash. Posts will look like this: [{_id: 'id1', ...}, {_id: 'id2', ...}, {...}] but what you really want is this: {id1: {_id: 'id1', ...}, id2: {_id: 'id2', ...}}. Does Angular do this for you automatically? Commented Dec 9, 2015 at 15:01
  • @Mike where do you see an array being used as an object hash? Commented Dec 9, 2015 at 15:03
  • @DmytroShevchenko Nevermind, I was thinking index was the ObjectID from Mongo, but it's an Angular thing. However, how does Angular guarantee post order since index can refer to different Posts? Commented Dec 9, 2015 at 15:08
  • @Mike ng-repeat="post in posts" iterates through posts in the order in which they were returned from MongoDB. If the query had sorting, it will be preserved. Commented Dec 9, 2015 at 15:10

1 Answer 1

1

Here's the code you have in the $scope.update():

var post = $scope.posts[index].etat; // note that you take just one field named "etat".
Posts.update({id: post._id}, post); // note that you use "id" instead of "_id".

Do you want to update only the etat field? Then you need to do this instead:

var etat = $scope.posts[index].etat;
Posts.update({ _id: post._id }, { "etat": etat });

Do you want to update the whole post? Then you need to pass the whole post and not only the etat field:

var post = $scope.posts[index];
Posts.update({ _id: post._id }, post);

Please note that your methods $scope.update() and $scope.passer() use id instead of _id, so you're going to have to fix that as well.

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

9 Comments

thanks this solution : var post = $scope.posts[index]; Posts.update({ id: post._id }, post); Is the solution I used in first time but I try it and nothing change on my dbb
@VaudeyBaptiste as I have mentioned in my answer, you also need to change id to _id.
Is it possible that I have some conflict with the other function that I use in the tutorial? She is in another controller, it's the same : $scope.update = function(index){ var todo = $scope.todos[index]; Todos.update({id: todo._id}, todo); $scope.editing[index] = false; }
@VaudeyBaptiste I cannot debug your code for you. If my answer does not help you, please create a Minimal, Complete, and Verifiable example instead of a lot of code that you provided in your question.
Yes I understand but I really think it's a dumb error and I don't find the issue
|

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.