题主初学node+mongodb,使用了mongoose模块,在取数据时出现了问题,我想获得数据库中某个collection的所有数据,但是取得的是空数组
//数据库连接和部分路由代码,不相关的部分都省略了
var Comment = require('./models/comment');
mongoose.connect('mongodb://localhost:27017/nodetest')
app.get('/api/comments',function(req, res){
Comment.fetch(function (err,data) {
if(err){
console.log(err);
}
res.send(data);
})
})
//model代码,这两段代码在不同的文件中
var mongoose = require('mongoose');
var CommentSchema = new mongoose.Schema({
author: String,
text: String
});
CommentSchema.statics = {
fetch: function(cb){
return this
.find({})
.exec(cb)
}
// findById: function(id,cb){
// return this
// .findOne({_id: id})
// .exec(cb)
// }
}
var Comment = mongoose.model('Comment',CommentSchema);
module.exports = Comment;
数据库名字为nodetest,其中有一个叫做comment的collection,里面放了若干数据,数据属性为author和text