findById 不返回结果。每次我登录时 const course
对象都是 null。
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/mongo-exercises', { useNewUrlParser: true });
const courseSchema = new mongoose.Schema({
name: String,
author: String,
tags: [ String ],
date: Date,
price: Number,
isPublished: Boolean
});
const Course = mongoose.model('Course', courseSchema);
async function updateCourse(id){
const course = await Course.findById(id);
if(!course) return;
course.isPublished = true;
course.author = 'Another Author';
const result = await course.save();
console.log(result);
}
updateCourse('5a68fde3f09ad7646ddec17e');
最佳答案
问题已解决。我注意到,当我在 .findById()
更新之前导入集合时;对象的 id 被保存为字符串而不是 ObjectID
关于node.js - Mongoose findById 查询不返回结果。对象始终为空。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52702025/