【问题标题】:Unable to update a User Record in nodejs/mongodb无法更新 nodejs/mongodb 中的用户记录
【发布时间】:2019-10-26 16:57:09
【问题描述】:

我正在尝试从 ionic 3 应用程序更新 mongodb 中的用户模型,但它不起作用 我得到的只是

请求方法:OPTIONS

状态码:204 无内容

客户端代码:

 return this.http.put(this.serveBaseUrl+'/user/update/'+params._id, params).pipe(map((res : any) => {

  console.log('here is',res)
   return res;
 }))

我的服务器端代码是

router.put('/update/:userId',controller.update_a_userModel);

Controller

User.findOneAndUpdate({ _id: req.params.userId }, req.body, { new: true }, function (err, users) {
        if (err)
            res.send(req.params.userId);
    res.json(req.params.userId);
  });

作为响应,我得到一个空值,用户记录保持不变。

任何帮助将不胜感激。

【问题讨论】:

    标签: node.js angular mongodb ionic3


    【解决方案1】:

    我相信您只是将 JSON 发送到您的后端。 试试下面:

    User.findOneAndUpdate({ _id: req.params.userId }, {$set:req.body}, { new: true }, function (err, users) {
            if (err)
                res.send(req.params.userId);
        res.json(req.params.userId);
      });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-28
      • 1970-01-01
      • 2017-06-24
      • 1970-01-01
      • 1970-01-01
      • 2021-05-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多