It appears that when I run mongoose in this code, it doesn't seem to connect to my local MongoDB database in time. The error message "mongooseError: Operation users.insertOne() buffering timed out after 10000 ms" is displayed if the insert operation is not commented out. Instead of receiving the expected "mongoose has been connected" message, only the aforementioned error is logged.
//script.js
const mongoose = require('mongoose')
const User = require("User")
mongoose.connect("mongodb://localhost/bh_db",
()=>{
console.log("mongoose has been connected")
}, e => console.error(e))
const user = new User({name: "Kyle", age: 26})
user.save().then( () => console.log("User Saved"))
//User.js
const mongoose = require('mongoose')
const userSchema = new mongoose.Schema({
name: String,
age: Number
})
module.exports = mongoose.model("User", userSchema)
If I comment out the insertion of a new user, eventually it connects to bh_db. Does anyone have insights on what might be causing this delay and potential solutions?