簡體   English   中英

無法更新 MongoDB 中的文檔

[英]Can't update document in MongoDB

所以我試圖簡單地更新我的數據庫中的一個文檔,我從數據庫中獲取數據沒有問題。 這是我的代碼:

const mongoose = require("mongoose");
    mongoose.connect("mongodb://localhost/mongo-exercises", {
      useNewUrlParser: true,
      useUnifiedTopology: true,
    });

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

//Creating model which returns a Class
const Course = mongoose.model("courses", courseSchema);


async function updateData(id) {
  try {
    const course = await Course.findById(id);

    course.isPublished = true;
    course.author = "another author";

    const resulti = await course.save();
    console.log(result);
  } catch (error) {
    console.log(error.message);
  }
}
updateData("5a68fdf95db93f6477053ddd");

我收到的錯誤:

Cannot set property 'isPublished' of null

提前感謝任何指針:)

我找到了解決方案,我使用的數據庫已棄用了 id:s 的格式。

//In my database
_id: "5a68fdd7bee8ea64649c2777"

//How it should look
_id: ObjectID("5a68fdd7bee8ea64649c2777")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM