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
[{_id: 'id1', ...}, {_id: 'id2', ...}, {...}]but what you really want is this:{id1: {_id: 'id1', ...}, id2: {_id: 'id2', ...}}. Does Angular do this for you automatically?indexwas the ObjectID from Mongo, but it's an Angular thing. However, how does Angular guarantee post order since index can refer to different Posts?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.