json - Node.js, Express, Mongodb using monk database empty -


i having problem while following tutorial in node.js using mongodb database monk. when run database command in mongo console can see database nodetest1 has 3 entries, when doing returns nothing.

router.get('/', function(req, res, next) {     var db = req.db;     var collection = db.get('usercollection');     collection.find({},{},function(e,docs){         res.send(docs);     }); }); 

and here print mongo console when use find method.

> db.usercollection.find().pretty() {         "_id" : objectid("55b17a551efb190c06c38d31"),         "username" : "testuser1",         "email" : "testuser1@testdomain.com" } {         "_id" : objectid("55b17a5f1efb190c06c38d32"),         "username" : "testuser2",         "email" : "testuser2@testdomain.com" } {         "_id" : objectid("55b17a5f1efb190c06c38d33"),         "username" : "testuser3",         "email" : "testuser3@testdomain.com" } 

if need more information give you. hope can me, have been stuck on problem 45 mins trying find solution.

update: how started mongod

mongod --dbpath "c:\users\victor\onedrive\programming\nodejs\databasesetup\data" 

and connection database in app.js:

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

when run program @ localhost:3000/userlist no data database console writes this:

2015-07-24t03:13:28.127+0200 network  [initandlisten] connection accepted 127.0.0.1:57961 #151 (6 connections open) 

going information have listed above i'm willing chance database connection strings not work in way think do.

you youself starting instance of mongod server this:

mongod --dbpath "c:\users\victor\onedrive\programming\nodejs\databasesetup\data" 

then can connect shell, i.e:

mongo 

and fine.

the problem here "database" connecting default , without other declaration called "test" , not name path in store physical data files.

therefore connect monk data do:

var db = require('monk')('localhost/test'); 

which connecting same database name data stored.

if wanted create different database name do:

mongo > use databasesetup 

or command line:

mongo databasesetup 

in either case switches default database contained. can create data in namespace.

but otherwise data resides in "test", should using.


just re-read first sentence there. perhaps database container name "nodetest1" since state, therefore:

var db = require('monk')('localhost/nodetest1'); 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -