【问题标题】:NodeJs MongoDB Update ErrorNodeJs MongoDB 更新错误
【发布时间】:2018-06-18 15:13:17
【问题描述】:

我的模型架构

const UserSchema = mongoose.Schema({
  name: {
    type: String,
    required: true
  },
  email: {
    type: String,
    required: true
  },
  username: {
    type: String,
    required: true
  },
  password: {
    type: String,
    required: true
  },
  tweets: []
});

这是我用来与 mongo 交流的方法

module.exports.getUserByUsername = function(username, callback){
  const query = {username: username}
  User.findOne(query, callback);
}
module.exports.addTweet = function(newTweet, newUser, callback){
  User.updateOne(newUser, {$push: newTweet}, (err, isUpdate) => {
    if(err) throw err;
    callback(null, isUpdate)
  });
}

我正在使用 NodeJS 编写我的后端代码,我已经注册了一个用户和一个登录名,但是当我尝试向该用户发布一条推文时,我收到了一个与 _id 相关的错误,并且我从不使用 id。

router.post('/post', passport.authenticate('jwt', {session:false}), (req, res, next) => {
  let newTweet = new User({
    tweets:{
      title: req.body.title,
      body: req.body.body
    }
  })
  User.getUserByUsername(req.body.username, (err, usert) => {
    if(err) throw err;
    if(!usert){
      return res.json({success: false, msg: 'User not found'});
    }
    User.addTweet(newTweet, usert, (err, isUpdate) =>{
      if(err) throw err;
      if(isUpdate){
        return res.json({success: true, msg: "Tweet Post"});
      }
    });
  });
});

错误

这是我使用 PostMan 时遇到的错误

/home/daniel/react/miapp/Back/node_modules/mongodb/lib/utils.js:132
      throw err;
      ^
MongoError: The field '_id' must be an array but is of type objectId in document {_id: ObjectId('5b26b4e911c67c4cfa6917e4')}
    at Function.MongoError.create (/home/daniel/react/miapp/Back/node_modules/mongodb-core/lib/error.js:45:10)
    at toError (/home/daniel/react/miapp/Back/node_modules/mongodb/lib/utils.js:149:22)
    at /home/daniel/react/miapp/Back/node_modules/mongodb/lib/collection.js:1035:39
    at /home/daniel/react/miapp/Back/node_modules/mongodb-core/lib/connection/pool.js:541:18
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

【问题讨论】:

  • 你应该使用_id更新用户User.updateOne({_id: newUser._id}, {$push: newTweet}, (err, isUpdate) => {
  • 我得到同样的错误 MongoError: The field '_id' must be an array but is of type objectId in document {_id: ObjectId('5b26b4e911c67c4cfa6917e4')}
  • 尝试定义这样的推文tweets:[{ title: String, body: String }]
  • 发生同样的错误,我什至不知道为什么问题与 id 有关

标签: javascript node.js mongodb mongoose mongoose-schema


【解决方案1】:

您的 getUserByUsername() 从 mongo 集合返回一个文档,例如

{_id: Object("...."), .....}

如果您只想要用户名,请将项目查询添加到您的 getUserByUsername() 为:

const project = {_id:0,username:1}
User.findOne(query, project,callback)

这仅返回文档的用户名。

还将新推文的定义更改为:

let newTweet = {tweets: {title: req.body.title,body: req.body.body}}

编辑:您还可以做的是让您的 getUserByUsername 代码像以前一样更改您的 updateOne 代码(如上所述定义 newTweet):

User.updateOne({_id: newUser._id}, newTweet, callback)

理想情况下,您应该只投影 mongo 集合中的 _id 并在更新时对其进行查询,因为它不仅可以让您免于检索不必要数据的整个网络,而且由于索引,更新查询也很快。

【讨论】:

  • 我真的很想感谢你,我整整一个星期都被那个小错误弄得头晕目眩。答案是:让 newTweet = {tweets: {title: req.body.title,body: req.body.body}}
  • 对不起哥们,我忘记了
猜你喜欢
  • 2015-11-10
  • 1970-01-01
  • 2014-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多