我可以使用mongo命令在終端(如db.getCollection('catalog').find({user_id:"56703f1a2fc76861e6a8743e"})
)中檢索我的數據,但是當我嘗試在nodejs中得到空值時。無法通過id使用mongodb獲取數據
我的路線是這樣的
var Catalog = require('../models/catalog');
router.get('/', function(req, res) {
if (!req.user) {
res.redirect('/login');
} else{
Catalog.getAllCatalog('56703f1a2fc76861e6a8743e', function(catalog){
console.log(catalog) //null
res.render('catalog/index');
});
}
});
我的目錄
var Catalog = module.exports = mongoose.model('catalog', catalogScheme);
module.exports.getAllCatalog = function(user_id, callback){
Catalog.find({"user_id":user_id}, callback);
}
任何思想的模型?
您確定配置了Mongoose模式,因此它使用'catalog'集合嗎?默認情況下,它將使用'catalogs'(複數)([documentation](http://mongoosejs.com/docs/guide.html#collection))。另外,你是否在你的模式中聲明'user_id'是一個字符串?也許你應該在你的問題中包含你的模式。 – robertklep
@robertklep沒有拼寫錯誤。這裏的模式看起來像http://pastebin.com/B5kgZ0Dn。還有一件事,mongoose.model('catalog')意味着你的集合必須被命名爲'catalog'吧? –