简体   繁体   English

如何从mongo获取数据并将其存储在玉文件中的变量中

[英]How to get data from mongo and store it in a variable in jade file

I am using express.js for my application, i want to retrieve data from my mongodb and store it in a variable in my jade file, then i will use this variable to draw charts using dygraph. 我正在为我的应用程序使用express.js,我想从mongodb中检索数据并将其存储在我的jade文件中的变量中,然后我将使用此变量通过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 

. i am getting error in last line after each and it says "expecting newline or semicolon". 我在每行之后的最后一行出现错误,并且显示“期望换行或分号”。 And below is the javascript file for routing. 下面是用于路由的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}); }); } 

I searched the internet but i am not getting any relevant data, and as a guide for node,mongo and express i am following this tutorial : cwbuecheler.com/web/tutorials/2013/node-express-mongo/ 我搜索了互联网,但没有得到任何相关数据,作为node,mongo的指南,我表示正在关注此教程:cwbuecheler.com/web/tutorials/2013/node-express-mongo/

In your docs either put _id as a string or remove that, that's an invalid json, test it at http://codebeautify.org/jsonvalidate , make it to string. 在您的文档中,要么将_id用作字符串,要么将其删除,这是无效的json,请在http://codebeautify.org/jsonvalidate对其进行测试,并将其设置为字符串。

Then try this: 然后试试这个:

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']

And if you want it in graphdiv put JavaScript code as it is, you may test it here http://jade-lang.com/ 如果您希望在graphdiv中按原样放置JavaScript代码,则可以在http://jade-lang.com/上对其进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM