使用node.js和angular js在MEAN Stack中无法使用更新功能

时间:2016-12-20 06:17:24

标签: javascript angularjs node.js mongodb

您好我是平均堆栈的初学者,我想使用更新

更新记录

功能我成功保存了记录,但更新功能无效。

我想根据id更新特定对象。但它没有用。

我正在使用此功能更新节点js文件

中的记录
  module.exports.useredit = function (req, res)
    {
        var user = new Usermodel();
        user = req.body.dataToModify;
        console.log(user);
        user.update({ _id:user._id },user);
    };

当我使用console.log(user);更新数据时,显示如下

{ _id: '5846692617e0575c0e0c2211',
  password: 123456,
  email: 'shahjad.ahmad89@gmail.com',
  name: 'shahjad232324324324',
  __v: 0 }

但数据未更新。我使用

时显示的错误消息
 user.update({ _id:user._id },user);

错误是

user.update is not a function</h1>↵<h2></h2>↵<pre>TypeError: user.update is not a function↵    at module.exports.useredit (C:\Users\Ahmad\ticket\controller\usercontroller.js:102:10)↵    at Layer.handle [as handle_request] (C:\Users\Ahmad\ticket\node_modules\express\lib\router\layer.js:95:5)↵    at next (C:\Users\Ahmad\ticket\node_modules\express\lib\router\route.js:131:13)↵    at Route.dispatch (C:\Users\Ahmad\ticket\node_modules\express\lib\router\route.js:112:3)↵    at Layer.handle [as handle_request] (C:\Users\Ahmad\ticket\node_modules\express\lib\router\layer.js:95:5)↵    at C:\Users\Ahmad\ticket\node_modules\express\lib\router\index.js:277:22↵    at Function.process_params (C:\Users\Ahmad\ticket\node_modules\express\lib\router\index.js:330:12)↵    at next (C:\Users\Ahmad\ticket\node_modules\express\lib\router\index.js:271:10)↵    at Function.handle (C:\Users\Ahmad\ticket\node_modules\express\lib\router\index.js:176:3)↵    at router (C:\Users\Ahmad\ticket\node_modules\express\lib\router\index.js:46:12)↵    at Layer.handle [as handle_request] (C:\Users\Ahmad\ticket\node_modules\express\lib\router\layer.js:95:5)↵    at trim_prefix (C:\Users\Ahmad\ticket\node_modules\express\lib\router\index.js:312:13)↵    at C:\Users\Ahmad\ticket\node_modules\express\lib\router\index.js:280:7↵    at Function.process_params (C:\Users\Ahmad\ticket\node_modules\express\lib\router\index.js:330:12)↵    at next (C:\Users\Ahmad\ticket\node_modules\express\lib\router\index.js:271:10)↵    at C:\Users\Ahmad\ticket\node_modules\express\lib\router\index.js:618:15</pre>↵"

如何重新获得它以及如何将数据更新为mongodb。

3 个答案:

答案 0 :(得分:1)

试试这个:

module.exports.useredit = function (req, res){ var id = req.params.id, body = req.body, options = { upsert: true, new : true } mongoDB.User.findOneAndUpdate({_id:id)}, body, options, function(err, user) { if(err){res.json(err)}else{res.json(user)} } }

注意,mongoDB.user.findOneAndUpdate,“mongoDB”是通用的。无论您将其命名为MongoDB Schema,这都是您的引用名称。

答案 1 :(得分:1)

您也可以尝试

my $message_body = <<END_OF_BODY;
<p>This is a list of things to bring</p>
<ul>
<li>Fresh flowers from <a href="http://www.example.com/flowers/">Flowershop X</a></li>
<li>Today's Arrangement instructions from <a href="http://www.example.com/arrangement/">instruction home page</a></li>
</ul>
END_OF_BODY

答案 2 :(得分:0)

试试这个..

user.findOneAndUpdate({_id:user._id}, user, {upsert:true}, function(err, doc){
    if (err) 
       return res.send(500, { error: err });
    return res.send("userdata updated successfully");
});

这里,“upsert = true”选项会创建对象(如果该对象不存在。)

默认为假。