Reputation: 1315
I'm desperately trying to solve my problem with "find" queries with mongoose in my app.
I begin to know very well the middleware, I used it on others apps and work well, but on a new project something goes wrong and it only finds null objects in my database...
//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);
Mongoose is well connected, there's no error in the console. So when mongoose is connected I use this method (I precisely use this in my LocalStrategy for PassportJS) :
User.findOne({username: "Paul"}, function (err, user){
console.log(user); // result : null
});
It's always null (and no error during the query), I also tested find({})
to check all entries, but the result is an empty array []
.
Here's the User collection of my pipeline database (checked with Robomongo):
/* 0 */
{
"_id" : ObjectId("547649df8d99c22fa995b050"),
"username" : "Paul",
"password" : "test"
}
/* 1 */
{
"_id" : ObjectId("54765efdcd3b13c80c2d03e2"),
"username" : "Micka",
"password" : "lol"
}
Thank you for your help.
Upvotes: 0
Views: 1042
Reputation: 311855
To have the 'User'
model use the User
collection, you need to explicitly provide that collection name as the third parameter to mongoose.model
, otherwise Mongoose will use the pluralized, lower-cased model name which would be users
.
module.exports = mongoose.model('User', userSchema, 'User');
Upvotes: 1