我正拼命地用我的应用程序中的猫鼬“查找”查询来解决我的问题。
我开始非常了解中间件,我在其他应用程序上使用了它,并且运行得很好,但是在一个新的项目中,有些地方出了问题,它只在我的数据库中找到空对象。
//server/app.js
var db = require('./lib/database');
var User = require('./models/user');
//server/lib/database.js
var mongoose = require('mongoose');
var db = function () {
mongoose.connect('mongodb://localhost/pipeline');
var connection = mongoose.connection;
connection.on('error', console.error.bind(console, 'connection error'));
connection.once('open', function callback() {
console.log('connected'); //result : connected
});
};
module.exports = db();
//server/models/user.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var userSchema = new Schema({ username: String, password: String });
module.exports = mongoose.model('User', userSchema);
猫鼬连接得很好,控制台中没有错误。因此,当猫鼬连接到时,使用这个方法(我在的LocalStrategy中精确地使用了这个方法):
User.findOne({username: "Paul"}, function (err, user){
console.log(user); // result : null
});
它总是null (在查询期间没有错误),我还测试了find({})
来检查所有条目,但是结果是一个空数组[]
。
下面是我的管道数据库的用户集合(用Robomongo检查):
/* 0 */
{
"_id" : ObjectId("547649df8d99c22fa995b050"),
"username" : "Paul",
"password" : "test"
}
/* 1 */
{
"_id" : ObjectId("54765efdcd3b13c80c2d03e2"),
"username" : "Micka",
"password" : "lol"
}
谢谢你的帮助。
发布于 2014-11-26 19:31:37
要让'User'
模型使用User
集合,您需要显式地将该集合名称作为mongoose.model
的第三个参数,否则Mongoose将使用复数、小写的模型名称,即users
。
module.exports = mongoose.model('User', userSchema, 'User');
https://stackoverflow.com/questions/27161287
复制相似问题