【问题标题】:Update data in mongoDB with Moongoose and AngularJs使用 Mongoose 和 AngularJs 更新 mongoDB 中的数据
【发布时间】: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


【解决方案1】:

这是您在$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".

您只想更新etat 字段吗?然后你需要这样做:

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

您想更新整个帖子吗?然后你需要传递整个帖子,而不仅仅是 etat 字段:

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

请注意,您的方法 $scope.update()$scope.passer() 使用 id 而不是 _id,因此您也必须修复它。

【讨论】:

  • 感谢这个解决方案:var post = $scope.posts[index]; Posts.update({ id: post._id }, post);是我第一次使用的解决方案,但我尝试了一下,我的 dbb 没有任何变化
  • @VaudeyBaptiste 正如我在回答中提到的,您还需要将id 更改为_id
  • 我在教程中使用的其他功能是否可能存在冲突?她在另一个控制器里,也是一样的:$scope.update = function(index){ var todo = $scope.todos[index]; Todos.update({id: todo._id}, todo); $scope.editing[index] = false; }
  • @VaudeyBaptiste 我无法为您调试代码。如果我的回答对您没有帮助,请创建一个Minimal, Complete, and Verifiable example,而不是您在问题中提供的大量代码。
  • 是的,我明白,但我真的认为这是一个愚蠢的错误,我没有找到问题
猜你喜欢
  • 1970-01-01
  • 2017-08-30
  • 1970-01-01
  • 2018-01-21
  • 1970-01-01
  • 2018-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多