Most probably it is because you have no documents in message
collection in db multivision
How Do I retrieve mongodb data using mongoose
Question
I am following a course on Pluralsight but this chunk of code is not working.Error in console:
mongoMessage = messageDoc.message; ^ TypeError: Cannot read property 'message' of null
mongoose.connect('mongodb://localhost:multivision');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error ...'));
db.once('open', function callback() {
console.log('multivision db opened');
});
var messageSchema = mongoose.Schema({message: String});
var Message = mongoose.model('Message', messageSchema);
var mongoMessage;
Message.findOne().exec(function(err, messageDoc) {
mongoMessage = messageDoc.message;
});
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow