【发布时间】:2015-12-09 22:57:33
【问题描述】:
我正在使用 NodeJS、AngularJS 和 MongoDB 以及 mongoose 来制作网站。我在修改 mongoDB 中的对象时遇到了一些麻烦。我遵循一个教程,一切都与这个例子一起工作。但是我尝试使用另一个表,新表的名称是 post,当我想更改表中的值时遇到问题。
这是我的模型:
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;
这里你可以看到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>
这是一个简单的 ng-repeat,它会显示我数据库中的每个帖子,如果我使用删除或保存功能一切正常,我可以在数据库中添加或删除帖子。
这是控制器:
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);
});
}
}])
这是后端的js:
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;
但是,如果我点击 edit($index) 并更改 post.corps 的值,然后点击 Confirmer,屏幕中的值会更改,但数据库中不会更改。
我认为我的更新功能有问题,但我不知道它是什么。 就像说,我已经完成了一个教程,并且在教程中创建了表,一切正常我可以修改一个值,但是我在这个新的表帖子中遇到了这个错误。
谢谢
【问题讨论】:
-
我不熟悉 Angular,但看起来您正在查询一个数组,就好像它是一个对象哈希一样。帖子看起来像这样:
[{_id: 'id1', ...}, {_id: 'id2', ...}, {...}],但你真正想要的是:{id1: {_id: 'id1', ...}, id2: {_id: 'id2', ...}}。 Angular 会自动为你做这件事吗? -
@Mike 你在哪里看到一个数组被用作对象哈希?
-
@DmytroShevchenko 没关系,我在想
index是来自 Mongo 的 ObjectID,但它是 Angular 的东西。但是,由于索引可以引用不同的帖子,Angular 如何保证帖子的顺序? -
@Mike
ng-repeat="post in posts"按照从 MongoDB 返回的顺序遍历帖子。如果查询有排序,它将被保留。
标签: angularjs node.js mongodb mongoose