EDIT : Duplicate of : Mongoose find() not returning result
I'm new to nodejs and nosql db.. Today I'm creating an API which read my user collection with two entries :
The problem is that the result is an empty array :
Here is the code :
The model :
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var UserModelSchema = new Schema({
_id: Schema.Types.ObjectId,
user_id:String,
age:{ type: Number },
status:String
});
module.exports = mongoose.model('user', UserModelSchema);
app.js :
//...
var User = require('./app/models/user');
//...
router.route('/users')
// get all the users
.get(function(req, res) {
User.find(function(err, users) {
if (err)
res.send(err);
res.json(users);
});
});
You may want to (even if not necessary) pass the query condition into your find
query:
For all users the condition would be: {}
User.find(<condition>, function(err, users) {
if (err)
res.send(err);
res.json(users);
});
But I found that if the collection was created through some method there is a chance that, your find
might not work. Mongoose find() not returning result
This can be the issue. Normal code-wise you are good. I would suggest you to delete of that collection and start by doing inserts from mongoose itself. This will be cleaner
Hope this helps !!
尝试这个...
User.find({}, function(err, docs) {}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.