简体   繁体   English

Node.js连接到mongodb

[英]Node.js connecting to a mongodb

I have been following this tutorial to try and get mongodb node.js, express, and jade to work together, but I can not for the life of me get the program to access any information from the database. 我一直在按照本教程尝试尝试使mongodb node.js,express和jade协同工作,但是我终生无法获得该程序来访问数据库中的任何信息。

here is the code in app.js that is supposed to access the database. 这是应该访问数据库的app.js中的代码。

var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/nodetest1');
....a few lines down....
app.use(function(req,res,next){
    req.db = db;
    next();
});

here is the code handling the routes: 这是处理路线的代码:

/* GET Userlist page. */
router.get('/userlist', function(req, res) {
    var db = req.db;
    var collection = db.get('usercollection');
    collection.find({},{},function(e,docs){
        res.render('userlist', {
            "userlist" : docs
        });
    });
});

And here is the code in userlist.jade: it is designed to purely figure out what the contents of userlist are. 这是userlist.jade中的代码:它旨在纯粹弄清楚userlist的内容是什么。 It is supposed to contain the information in the database (which has 3 entries with 2 pieces of data for each). 它应该包含在数据库中的信息(数据库中有3个条目,每个条目有2条数据)。 I have other jade code from an example online that runs and is essentially just a 'Hello World' program. 我从网上运行的示例中获得了其他玉器代码,该代码实际上只是一个“ Hello World”程序。

doctype html
html(lang="en")
  head
    title= pageTitle
  body
    #{userlist}

the page outputs to this: 页面输出到此:

<[object Object],[object Object],[object Object]>

This explains why I was getting errors looping through the data, but what I need help with is getting the program to actually get the proper information from the database. 这就解释了为什么我在遍历数据时会出错, 但是我需要帮助的是使程序实际从数据库中获取正确的信息。

Extra information: I can access the database through command prompt and view the data I put it. 附加信息:我可以通过命令提示符访问数据库,并查看放置的数据。 I am using Windows 8.1 我正在使用Windows 8.1

Update: code that makes it work! 更新:使它起作用的代码!

index.js (route handler): index.js(路由处理程序):

/* GET Userlist page. */
router.get('/userlist', function(req, res) {
    var db = req.db;
    var collection = db.get('usercollection');
    collection.find({},{},function(e,docs){
        res.render('userlist', {
            "userlist" : docs
        });
    });
});

userlist.jade userlist.jade

extends layout

block content
    h1.
        User List
    ul
        each user, i in userlist
            li
                a(href="mailto:#{user.email}")= user.username

I'm not sure exactly why this is working now because I did a lot of testing to make sure I was sending the right data over which include copying and pasting code from the tutorial, but it works now! 我不确定为什么现在可以正常工作,因为我做了很多测试以确保发送正确的数据,包括从教程中复制和粘贴代码,但是现在可以正常工作了!

Edit: I got closer to the solution by confirming that it is in fact pulling data from the server, just tested it and verified with the tutorial the code I should be using and it is now functioning properly. 编辑:通过确认它实际上是从服务器中提取数据,我进行了测试,并通过本教程验证了我应该使用的代码,并且该代码现在可以正常运行,从而更接近该解决方案。 Added proper code. 添加了正确的代码。

I believe you are getting this output because docs in the callback is an array. 我相信您会收到此输出,因为回调中的文档是一个数组。 Can you loop through docs and simply print individual documents to the console? 您可以遍历文档并仅将单个文档打印到控制台吗? This will verify that the data access part is working, and you can figure out HTML output afterwards. 这将验证数据访问部分是否正常工作,然后可以确定HTML输出。

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

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