My code
const mongoose = require('mongoose'); const Tutorial = require('./models/Tutorial'); async function createTutorial() { try { console.log('guy Incognito'); const doc = await Tutorial.create({ title: 'bill ', body: 'Bill Russel was great player!' }); console.log(`randomTextabcden${doc}`); } catch (err) { console.log(err); } } createTutorial();
In models/Tutorials
const mongoose = require('mongoose'); const TutorialSchema = new mongoose.Schema({ title: String, body: String, createdAt: { type: Date, default: Date.now, }, }); module.exports = mongoose.model('Tutorial', TutorialSchema);
When I run my code from terminal,
node index.js
shows
guy Incognito
It seems that promise is not resolved.. Why?
Advertisement
Answer
You probably don’t have a correct connection to your database. If I take your exact code, everything is working fine with a correct connection and behave as described in your question with no connection.
To create a connection call mongoose.connect('mongodb://<db_uri>', {useNewUrlParser: true});
somewhere in your app.
To understand why your code didn’t throw any error and you had no log, you can read more about how mongoose handle connection here