如何修复MongooseError:mongoose中的操作'fruits.insertOne()'缓冲在10000ms后超时?

sirbozc5  于 2022-11-13  发布在  Go
关注(0)|答案(1)|浏览(154)

你好我是一个初学者开发人员,我是以下教程udemy关于mongoose,不幸的是教程是过时的,所以我不得不写它的文档.这是我的代码:

//require the Mongoose package (after running >npm i mongoose in Hyper to install it)
const mongoose = require('mongoose');
 
//connect to MongoDB by specifying port to access MongoDB server
main().catch(err => console.log(err));
 
async function main() {
  await mongoose.connect('mongodb://localhost:27017/FruitsDB');
  }
 

    //create a SCHEMA that sets out the fields each document will have and their datatypes
    const fruitSchema = new mongoose.Schema ({
        name: String,
        rating: Number,
        review: String
    })
     
    //create a MODEL
    const Fruit = new mongoose.model ("Fruit", fruitSchema)
     
    //create a DOCUMENT
    const fruit = new Fruit ({
        name: "Apple",
        rating: 7,
        review: "Great!"
    })
     
    //save the document
    fruit.save()
     
    //**CHALLENGE: Set up a people database with one document and two fields**//
    //create a SCHEMA
    const personSchema = new mongoose.Schema({
      name: String,
      age: Number,
    });
     
    //create a MODEL
    const Person = mongoose.model('Person', personSchema);
     
    //create a DOCUMENT
    const person = new Person({
      name: "John",
      age: 37
    });
     
    //Save it
    person.save();

但是控制台只返回一个错误消息,说:

MongooseError: Operation `fruits.insertOne()` buffering timed out after 10000ms

我已经试着修了几个小时了,如果你能帮忙那就太好了

gijlo24d

gijlo24d1#

此错误通常在未连接到数据库时发生。请确保在尝试执行任何操作之前确实已连接到数据库。

相关问题