Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Mongoose findById查询不返回结果。对象始终为空。_Node.js_Mongodb_Mongoose_Mongodb Query - Fatal编程技术网

Node.js Mongoose findById查询不返回结果。对象始终为空。

Node.js Mongoose findById查询不返回结果。对象始终为空。,node.js,mongodb,mongoose,mongodb-query,Node.js,Mongodb,Mongoose,Mongodb Query,findById不返回结果常量课程每次我登录时对象都为空 const mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/mongo-exercises', { useNewUrlParser: true }); const courseSchema = new mongoose.Schema({ name: String, author: String, tags: [ Stri

findById不返回结果<代码>常量课程每次我登录时对象都为空

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

null
表示找不到具有该
\u id的文档。你能更新你的问题以包含你期望找到的文档吗?到数据库的连接是可疑的。您不想在代码看起来正常的localhost位之后添加“:27017”(或mongod正在侦听的任何端口)吗。我测试了完全相同的函数体+
findById
save
,只是使用了不同的模式。你需要提供一些样本数据