如何将MongoDB中的对象发送给jade? [英] How do I send on object from MongoDB to jade?

查看:98
本文介绍了如何将MongoDB中的对象发送给jade?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问我使用客户端JS从MongoDB检索的对象。具体来说,我希望能够遍历并使用对象中的数组。这是我的服务器端JS,它成功找到结果并将它们记录到终端。

I'm trying to access an object that I retrieve from MongoDB with my client-side JS. Specifically, I want to be able to loop through and use the arrays within the object. Here is my server-side JS which successfully finds the results and logs them to 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 });
  })
})
})

我的玉:

  #luck
   #{results}

然后我的客户端JS:

var results = $('#luck').html();
console.log(results.answers[2]);

我使用console.log获得 undefined 并将结果打印在页面上。

I get undefined with the console.log and the results are printed on the page.

推荐答案

感谢@Xinzz的答案,但是这个玉:

Thanks for the answer @Xinzz, but this jade:

for result in results
     p #{result.answers}

最终为我工作。从这里:使用Jade迭代JSON

ended up working for me. From here: Using Jade to iterate JSON

这篇关于如何将MongoDB中的对象发送给jade?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆