Domanda

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;
});
È stato utile?

Soluzione

Most probably it is because you have no documents in message collection in db multivision

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top