【问题标题】:How to use .save method in mongo?如何在 mongo 中使用 .save 方法?
【发布时间】:2017-11-02 03:26:19
【问题描述】:
 app.put('/app/modify/:_id', function(req, res) {
        Collection.findById(req.params._id, function (err, blog) {
            if (err) res.send(err);

            if (req.body.text1) blog.text1 = req.body.text1;
            if (req.body.text2) blog.text2 = req.body.text2;
            if (req.body.text3) blog.text3 = req.body.text3;
            if (req.body.text4) blog.text4 = req.body.text4;
            if (req.body.text5) blog.text5 = req.body.text5;

            Collection.save( function (err) {
                if (err) send (err);
                res.json({message: 'Blog Post Updated!'});
            });
        });
    });

从中获得帮助 - PUT and DELETE - RESTful API Express MongoDB Mongoose

但出现错误 - http://localhost:8080/app/modify/59203c7d9532c34903000002 net::ERR_EMPTY_RESPONSE 并且节点服务器停止并出现错误“Collection.save 不是函数”。

【问题讨论】:

  • 你的意思是blog.save(function(err) { ...。这是一个实例方法。恕我直言,根本不应该使用它,但这是一个完全不同的主题。

标签: node.js mongodb mean-stack


【解决方案1】:

试试看……

 app.put('/app/modify/:_id', function(req, res) {
            Collection.findById(req.params._id, function (err, blog) {
                if (err) res.send(err);

                if (req.body.text1) blog.text1 = req.body.text1;
                if (req.body.text2) blog.text2 = req.body.text2;
                if (req.body.text3) blog.text3 = req.body.text3;
                if (req.body.text4) blog.text4 = req.body.text4;
                if (req.body.text5) blog.text5 = req.body.text5;

                blog.save( function (err,response) {
                    if (err) res.json(err);
                    res.json({message: 'Blog Post Updated!'});
                });
            });
        });

【讨论】:

  • 谢谢,现在它正在工作,但数据没有得到更新。没有显示任何错误。
猜你喜欢
  • 2014-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-30
  • 2014-07-04
  • 2017-08-04
相关资源
最近更新 更多