Comment
Author: Admin | 2025-04-27
Before you use npm run startThe mongoose code does not have any syntax errorsUse promises .then() in the connect function for mongooseIf you are using localhost in URL, try switching it with 0.0.0.0 or vice versaOnce you have done all this, run the command node run start and hopefully this should work now. To verify, go to mongoDB and see the newly created database.Below is a sample code for MongoDB and Mongoose that worked for me.const mongoose = require('mongoose');const myFunc = () => { const userSchema = mongoose.Schema({ name: String }); const User = mongoose.model("User", userSchema); const userObject = new User({ name: 'Abhishek' }); userObject.save((err, data) => { if (err) console.log('Error in saving = ' + err); if (data) console.log('Saved to DB = ' + data) } );};mongoose .connect("mongodb://0.0.0.0/userdb") .then(myFunc(), err => console.log(`Error = ${err}`)); answered Jan 8, 2022 at 8:55 Please ensure that your mongo DB is set Automatic and running at Control Panel/Administrative Tools/Services like below. That way you wont have to start mongod.exe manually each time. answered Apr 26, 2020 at 7:06 wise kingwise king491 silver badge7 bronze badges 2
Add Comment