How to fetch data from mongodb and show using node.js -
following code fetch data collection , show on index page, not giving results.
node code-
var app = require('express')(); var mongoose = require('mongoose'); var dburi = 'mongodb://localhost/test'; mongoose.connect(dburi); var testschema = new mongoose.schema({ name: string, rollnum: string }); var test = mongoose.model('test', testschema); app.get('/', function(req, res){ test.find({},function(err, docs){ res.send('index',{docs:docs}); }); //res.send('test'); }); app.listen(3001);
however check , have collection in db -
query fire - db.testinfo.find()
output -
{ "_id": objectid("123456..78"), "name": "test", "rollnum": "xxxx" }
after hitting url - http://127.0.0.1:3001/
this output getting -
{ "docs": [] }
however expecting result of name, rollnum.
please let me know doing wrong.
when register model in mongoose, uses pluralized, lower-cased model name name of collection it's tied to. because model name test
, collection name tests
.
to tie model testinfo
instead, pass name third parameter model
call:
var test = mongoose.model('test', testschema, 'testinfo');
Comments
Post a Comment