我正在为我的小气应用程序创建一个行走骨架。我已成功连接到MongoDB(Multivision DB已打开),但继续,我在尝试将消息打印到浏览器窗口时遇到标题中描述的错误:
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= new Message({message: 'Hello mongoDB here'});
console.log(mongoMessage.message);
Message.findOne().exec(function(err, messageDoc) {
mongoMessage = messageDoc.message;
})
app.get('*', function(req, res) {
res.render('index', {
mongoMessage: mongoMessage
});
});
最佳答案
您需要先保存mongoMessage
,然后才能查询它:
mongoMessage.save(function(err, doc) {
Message.findOne().exec(function(err, messageDoc) {
mongoMessage = messageDoc.message;
});
});