I'm following a simple tutorial .
Thing is, I'm using momngoose instead of Mongo. I had no problems until I get to this point:
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})
})
})
With this, the quotes can be accessed and manipulates on index.ejs
now, I tried doing this:
app.get('/theroute', (req, res) => {
MyMongoModel.find()
.then(documents => {
res.render('index.ejs', *not entirely sure on what to put here*)
})
});
but when I tried to use the documents on the index.ejs page, I got a "not defined" result.
And that's it. No sure on how to google this or what should I do.
Thanks!
Try the following:
MyMongoModel.find({}, (err, foundQuotes) => {
if(err) return console.log(err);
res.render('index.ejs', {quotes: foundQuotes});
}
The first argument passed into find()
, the {}
, will retrieve all database entries matching the particular model, and passes that collection into the callback function ( find()
's second argument), as foundQuotes
.
In the callback function, index.ejs
is rendered (if there are no errors) and foundQuotes
is assigned to a variable called quotes
. quotes
will then be accessible in your index.ejs
file.
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.