Whenever I try to access an object fetched from MongoDB using my client-side JS, I encounter a challenge. Specifically, my goal is to iterate through and utilize the arrays stored within the object. Below is the server-side JS code which effectively retrieves the results
and outputs them in the terminal.
app.get("/post/:id", function (req, res, next) {
var id = req.param('id');
var query = BlogPost.findById(id).populate('author');
BlogPost.find(query, {"answers":"true", "blanks":"true", "_id":0}, function(err, results){
console.log('Results '+results);//This prints an object like:// { answers: [ 'more', 'One', 'more' ], blanks: ['try']}
query.exec(function (err, post) {
if (err) return next(err);
if (!post) return next(); // 404
res.render('post/view.jade', { post: post, results: results });
})
})
})
Additionally, here is the jade code snippet:
#luck
#{results}
Next, let's take a look at my client-side JS code:
var results = $('#luck').html();
console.log(results.answers[2]);
While the results are correctly displayed on the page, I am unfortunately receiving undefined
when I try to log the third answer array element.