嘿我想从我的index.js文件中查询数据库(mongodb),然后将查询中的值返回到变量中,以便我可以使用该变量在网站上显示个人信息。我目前有这个代码,但它给我的问题。试图找出如何以正确的方式来查询它。我正在查询电子邮件,但我想根据电子邮件查询提取名字。我为我的视图引擎使用node.js和jade。简单的db mongo查询出错了吗?
var mongoose = require('mongoose')
, db = mongoose.createConnection('mongodb://localhost/test5');
var user = function (name) {
var name = db.users.find({ email: '[email protected]' }).pretty();
console.log(name);
return name;
};
exports.index = function(req, res) {
res.render('index', { title: 'Weblio', user: user});
};
感谢的是工作,是的,我有一个用户模型不知道如果我可以用它这样。做res.send(err)或req.send(500)更好吗? – Lion789
您是否希望用户看到错误,取决于您。你可以对用户执行'res.send(500)',也许'console.log(err)'可以看到它。 – jrthib
有道理,对于这种情况或人们通常采用的路线,最佳做法是什么? – Lion789