mongoose.connect async await code example
Example 1: async await mongoose connection
(async () => {
try {
await mongoose.connect(dbURI, dbOptions)
} catch (err) {
console.log('error: ' + err)
}
})()
Example 2: node js mongoose async await
const connectToDb = async () => {
try {
await mongoose.connect(<connection_string>, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
});
console.log("Db connection success");
} catch (err) {
console.log(err.message);
process.exit(1)
}
};