2011-09-19 70 views
1

遇到問題,使用貓鼬從mongo獲取任何數據。連接看起來很好,因爲我有打印出來的調試語句。我搜索了什麼可能導致這個,但afaik高和低,我設置架構和收集很好。如何使用Mongoose從Mongo中獲取數據?

這裏就是我有一個名爲posts.js:

var mongoose = require('mongoose'); 

var db = mongoose.connect('mongodb://localhost:27017/sister', function(err) { 
    if (err) throw err;} //this does not get printed 
); 

mongoose.connection.on("open", function(){ 
    console.log("mongodb is connected")} //this gets printed 
); 

var Schema = mongoose.Schema; 
var thePost = new Schema({ 
     name : String 
}); 

mongoose.model('post', thePost); 
var posts = db.model('post'); 

posts.find({}, [], function(err, calls) { 
    console.log(err, calls, calls.length); //prints out: null [] 0 
}); 

種子數據,我在我的蒙戈外殼,裏面插入的文件這樣做,則表明找到所有可以找到它:

> randumb = { name : 'emile' }; 
{ "name" : "emile" } 
> db.post.insert(randumb); 
> db.post.find({});  
{ "_id" : ObjectId("4e775e8cc24f31883fdafbab"), "name" : "emile" } 

回答

0

嘗試改變

var posts = db.model('post'); 

var posts = mongoose.model('post'); 
0

爲了保持它更短,嘗試和變化:

mongoose.model('post', thePost); 
var posts = db.model('post'); 

到:

var posts = mongoose.model('post', thePost); 
相關問題