2017-04-24 60 views
0

我正在关注simple tutorial猫鼬等同于mongoDB查询

事情是,我正在使用momngoose而不是Mongo。 我没有问题,直到我得到了这一点:

app.get('/', (req, res) => { 
    db.collection('quotes').find().toArray((err, result) => { 
    if (err) return console.log(err) 
    // renders index.ejs 
    res.render('index.ejs', {quotes: result}) 
    }) 
}) 

这样,报价可以访问和现在index.ejs

操纵,我试着这样做:

app.get('/theroute', (req, res) => { 
    MyMongoModel.find() 
    .then(documents => { 
     res.render('index.ejs', *not entirely sure on what to put here*) 
    }) 
    }); 

但是当我试图使用index.ejs页面上的文档时,我得到了“未定义”的结果。

就是这样。不知道如何谷歌这或我该怎么做。

谢谢!

+0

您可能需要将您想要查找的内容传递给find()方法 – heyitsjhu

回答

0

尝试以下操作:

MyMongoModel.find({}, (err, foundQuotes) => { 
    if(err) return console.log(err); 
    res.render('index.ejs', {quotes: foundQuotes}); 
} 

传递到find()的第一个参数,该{},将检索匹配特定型号的所有数据库条目,并传递收集到的回调函数(find()的第二个参数) ,如foundQuotes

在回调函数中,将呈现index.ejs(如果没有错误)并将foundQuotes分配给名为quotes的变量。 quotes然后可以在您的index.ejs文件中访问。