【问题标题】:Mongoose update whole ArrayMongoose 更新整个数组
【发布时间】:2013-08-19 07:00:25
【问题描述】:

我正在使用 Mongoose 制作一个由 Backbonejs 驱动的应用程序。基本上我有一个考试模型,里面有一系列问题。因此,当用户回答问题时,我在端点上进行 PUT(使用 Backbonejs 的模型保存方法)来更新这一系列问题(我想覆盖它)。 但是当我这样做时,我收到一个错误。

这是我的代码:

Exam.findOneAndUpdate({_id: req.body._id}, {questions: req.body.questions},   function(err, exam){
    console.info(err)
    if(err) res.end();
    res.json(req.body)
})

console.info(err) 输出的地方: [TypeError: Object 0 has no method 'split']

这是我的对象(简化):

{
    exam: "First exam",
    id: 1,
   questions: [
       {
           question: "Where does babies come from?",
           answer: 1
       },
       {
           question: "What time is it?",
           answer: 3
       }
   ]
}

注意:当我尝试更新“exam”之类的字符串属性时,它会正常更新。

有人知道发生了什么吗?谢谢!!

【问题讨论】:

  • req.body.questions 的类型是什么?字符串还是数组?

标签: node.js mongodb mongoose


【解决方案1】:

我发现这是我使用的 TingoDb 的一个错误。感谢您的帮助!

【讨论】:

    猜你喜欢
    • 2020-09-13
    • 1970-01-01
    • 1970-01-01
    • 2017-01-24
    • 2019-03-25
    • 1970-01-01
    • 1970-01-01
    • 2018-12-21
    相关资源
    最近更新 更多