简体   繁体   中英

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.

here is the code in app.js that is supposed to access the database.

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. It is supposed to contain the information in the database (which has 3 entries with 2 pieces of data for each). I have other jade code from an example online that runs and is essentially just a 'Hello World' program.

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

Update: code that makes it work!

index.js (route handler):

/* 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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