【问题标题】:How to make a put request in Nodejs如何在 Nodejs 中发出 put 请求
【发布时间】:2020-10-27 04:05:54
【问题描述】:

我正在 node js 中编写一个 put 方法,当我想保存帖子时出现错误,它显示错误。

Post.findOne({'imei.modele': req.query.modele, test: { $exists: true } })

 .then((posts) => {
   console.log(posts);
posts=aa;
posts.save().then(posts=>{
  res.json('bonjour');
})
 })


});

我收到以下错误 - TypeError: posts.save is not a function request.find 工作正常,req.body 也不错 所以我需要一些帮助,谢谢你

【问题讨论】:

  • 我有一个很大的 req.BODy 我想更新所有的帖子有一个解决方案
  • 你为什么用aa重新分配帖子??? posts=aa

标签: node.js mongodb express mongoose mongodb-query


【解决方案1】:

您好,这种行为很明显,因为您使用 aa 重新分配了 posts 的值:

删除这个

posts=aa; 

这样试试

Post.findOne({'imei.modele': req.query.modele, test: { $exists: true } })

 .then((posts) => {
   console.log(posts);
posts.save().then(posts=>{
  res.json('bonjour');
})
 })


});

如果您想在保存之前编辑posts,请尝试这样的操作

Post.findOne({'imei.modele': req.query.modele, test: { $exists: true } })

 .then((posts) => {
   console.log(posts);
posts.modele = 'updated model'
posts.save().then(posts=>{
  res.json('bonjour');
})
 })


});

posts.modele = 'updated modele' 这样的地方应该更新modele 属性

更清洁的方式

Post.findOneAndUpdate({'imei.modele': req.query.modele, test: { $exists: true } }, { $set : { req.body } }).then(doc=>{
      res.json('success');
    })

【讨论】:

  • 我有一个很大的 req.BODy 我想更新所有的帖子有一个解决方案
  • 试试Post.findOneAndUpdate({'imei.modele': req.query.modele, test: { $exists: true } }, { $set : { req.body } })
猜你喜欢
  • 2015-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多