首页 > 解决方案 > 无法将文档上传到我的 mongoDB 服务器

问题描述

我正在研究自己的 node.js,并且正在关注 codewithmosh 网站。问题是,我尝试使用 compass 将新对象上传到我的 mongo 数据库。我有以下代码(请参阅屏幕截图以获取输出)并且没有错误,并且在 mosh'es 教程中一切正常。

我遇到的问题是我的服务器没有使用新文档进行更新。知道我缺少什么吗?

我刷新了几次指南针,没有任何变化。

在此处输入图像描述

在此处输入图像描述

const mongoose = require('mongoose');


mongoose.connect('mongodb://localhost/playground', { useNewUrlParser: true , useUnifiedTopology: true}).then(() =>
    console.log('connected to MongoDB')
).catch((err) => console / log('Could not connect to MongoDB', err));


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

const Course = mongoose.model('Course', courseSchema);
async function createCourse () {
    const course = new Course ({
        name: 'node.js Course',
        author: 'Daniel',
        tags: ['node', 'backend'],
        isPublished: true,

    });
    const result = await course.save();
    console.log(result);
}

createCourse();


标签: node.jsmongodbmongoose

解决方案


好的,我想通了。我的指南针配置错误。我不得不去建立一个连接


推荐阅读