【问题标题】:Why mongoose doesn't update?为什么猫鼬不更新?
【发布时间】:2019-08-10 15:38:05
【问题描述】:

我正在学习 Node.js。由于这个问题,我被困在这个 Mongoose 部分。 findById(id) 没有返回我的结果,并且 update() 不起作用。不知道为什么。。

const mongoose = require("mongoose").set("debug", true);
mongoose
  .connect("mongodb://localhost/exercise")
  .then(() => console.log("Connected Successfully!"))
  .catch(err => console.error("Error: ", err.message));

const courseSchema = new mongoose.Schema({
  tags: [String],
  date: { type: Date, default: Date.now() },
  name: String,
  author: String,
  isPublished: Boolean,
  price: Number
});

const Courses = mongoose.model("Course", courseSchema);

async function updateCourse(id) {

  const course = await Courses.findById(id);
  course.author = "Muhib";
  const result = await course.save();
  console.log(result);
}

updateCourse("5a68fde3f09ad7646ddec17e");
//  console.log(Courses);

我收到此错误:

TypeError: 无法将属性“作者”设置为 null

这是我记录的一个片段:

顺便说一句 find() 有效..

提前谢谢:)

【问题讨论】:

  • 尝试更新你的 const 课程,让课程进入 updateCourse 功能。
  • 感谢您的回答...我得到同样的错误

标签: node.js mongoose


【解决方案1】:

尝试添加try/catch。我不确定 course..save() 是否可以正常工作

async function updateCourse(id) {
    try {
        const course = await Courses.findById(id);
        course.author = "Muhib";
        const result = await course.save();
        console.log(result);
    } catch (err) {
        console.log('err' + err);
    }
}

编辑 1:我找到了一个解决方案 post

async function updateCourse(id) {
    try {
        const course = await Courses.findById(id).exec();//fixed at here
        course.author = "Muhib";
        const result = await course.save();
        console.log(result);
    } catch (err) {
        console.log('err' + err);
    }
}

使用 findOneAndUpdate

的另一种解决方案
try {
    var course = await Courses.findOneAndUpdate(
        {"id" : id},
        {$set: {"author" : "Muhib"}},
        {new : true}
    );
    res.status(200).json(course);
} catch (error) {
    console.log('err' + err);
}

【讨论】:

  • 其实是我的错。。我忘了说上面的错误了... TypeError: Cannot set property 'author' of null
  • 再次感谢.. 但问题不在我在解决方案中提到的代码中。
【解决方案2】:


我解决了这个问题。
代码没有问题...
第一件事 为什么它不起作用

只有导入的集合才会出现此问题。因为 导入的集合包含 _id 属性作为字符串

例如
{"_id":"5a6900fff467be65019a9001","tags":["angular","frontend"],"date":"2018-01-24T21:56:15.353Z","name":"Angular Course","author":"Mosh","isPublished":true,"price":15,"__v":0}

但是 mongoose 需要的是 _id 包裹在 ObjectId 中

解决方案 导入 json 对象时,请始终确保 _id 不是字符串,而是以 $oid 作为键和 _id 作为值的对象

例如
{"_id":"5a6900fff467be65019a9001","tags":["angular","frontend"],"date":"2018-01-24T21:56:15.353Z","name":"Angular Course","author":"Mosh","isPublished":true,"price":15,"__v":0}
应该是
{"_id":{"$oid":"5c91a66d079f4807847fcde3"},"tags":["angular","frontend"],"date":"2018-01-24T21:56:15.353Z","name":"Angular Course","author":"Mosh","isPublished":true,"price":{"$numberInt":"15"},"__v":{"$numberInt":"0"}}

【讨论】:

    猜你喜欢
    • 2013-03-15
    • 2020-11-13
    • 2022-12-18
    • 2012-03-28
    • 2016-05-24
    • 1970-01-01
    • 1970-01-01
    • 2020-10-23
    • 2016-05-14
    相关资源
    最近更新 更多