const mongoose = require("mongoose")
mongoose.connect("mongodb://localhost:27017/testdb")
const userSchema = new mongoose.Schema({
name : String,
age : Number
})
const User = mongoose.model("User", userSchema);
const user1 = new User({name: "Jack", age: 24});
user1.save().then(() => console.log("User Saved"));
I was trying to perform CRUD operations on mongoDB database using mongoose and this error popped up.
2
Answers
Establishing a connection is async operation.
You need to await for it and only then you’ll be able to run queries.
I had the same problem. I successfully got fine after see this answer, thank you.