【问题标题】:How to get data from mongo and store it in a variable in jade file如何从mongo获取数据并将其存储在jade文件中的变量中
【发布时间】:2016-05-12 17:04:44
【问题描述】:

我在我的应用程序中使用 express.js,我想从我的 mongodb 中检索数据并将其存储在我的翡翠文件中的一个变量中,然后我将使用这个变量来使用 dygraph 绘制图表。

doctype html
html
   head
      title siddh
      link(rel='stylesheet', href='/stylesheets/style.css')
      script(type='text/javascript', src='http://cdnjs.cloudflare.com/ajax/libs/dygraph/1.1.1/dygraph-combined.js')
   body
      #graphdiv(style="width:1000px; height=700px;")
      script(type='text/javascript').
          each doc, i in docs

。我在每一行之后的最后一行出现错误,它说“期待换行符或分号”。下面是路由的javascript文件。

exports.getData = function(req,res){

    var db = req.db;
    var collection = db.get('collection');
    collection.find({},{},function(e,docs){
        res.render('index1.jade',{docs:docs});
    });
}

我搜索了互联网,但没有获得任何相关数据,作为节点、mongo 和 express 的指南,我正在关注本教程:cwbuecheler.com/web/tutorials/2013/node-express-mongo/

【问题讨论】:

  • @chridam,好的,我会这样做,感谢您的宝贵意见
  • 在你的exports.getData中,快速检查一下文档是否不为空,尝试提供console.log(docs)并检查类似if(!e)的错误。之后在玉中尝试直接将文档像 p #{docs}
  • @aishwat singh,从控制台我得到如下信息:--> GET /stylesheets/style.css 304 0.421 ms - - [ { _id: 562dbf78a8c410c3ff306a3e, Date: '2015-10-24 ',TTFB:'845',加载时间:'5641'},{_id:562dc10ca8c410c3ff306a3f,日期:'2015-10-21',TTFB:'800',加载时间:'8703'},{_id:562dc10ca8c410c3ff306a40,日期: '2015-10-22', TTFB: '900', loadTime: '5758' } ] 这意味着我正在获取数据到 getData.js,现在我需要在我的玉文件中呈现它,以便我可以看到图表这个数据。

标签: javascript node.js mongodb express


【解决方案1】:

在您的文档中,要么将 _id 作为字符串,要么将其删除,这是一个无效的 json,在 http://codebeautify.org/jsonvalidate 对其进行测试,使其成为字符串。

然后试试这个:

doctype html
html
   head
      title siddh
      link(rel='stylesheet', href='/stylesheets/style.css')
      script(type='text/javascript', src='http://cdnjs.cloudflare.com/ajax/libs/dygraph/1.1.1/dygraph-combined.js')
   body
      #graphdiv(style="width:1000px; height=700px;")
      ul
         each val,i in docs
            li= val['_id']
            li= val['Date']
            li= val['TTFB']

如果你希望它在 graphdiv 中按原样放置 JavaScript 代码,你可以在这里测试它http://jade-lang.com/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-19
    • 2019-08-19
    • 1970-01-01
    • 1970-01-01
    • 2017-11-05
    • 1970-01-01
    • 2021-06-26
    相关资源
    最近更新 更多