javascript - getting an empty response returned from nodeJS + Express -


i apologize if not follow comunity standars, please note tried best.

i'm having troubles returning json response in api grabs content mongodb instance

basically have following file handles nodejs server (server.js):

var express = require('express'); var app = express(); var bodyparser = require('body-parser'); var mongoose = require('mongoose'); var person = require('./models/people');  mongoose.connect('mongodb://localhost/testdb');  app.use(bodyparser.urlencoded({ extended: true })); app.use(bodyparser.json());  var port = process.env.port || 8080; var router = express.router();  router.use(function (req,res,next) {     console.log('something going on');     next(); })  router.get('/', function (req,res) {     res.status(500).json({         error: 'unauthorized'     }); });  router.route('/getpeople').get(function (req,res){     person.find(function (err,people) {         console.log('err: ' , err);         console.log('people: ' , people);         if (err)             res.send(err)         res.json(people);     }) })  app.use('/api',router);  app.listen(port); console.log('server started on port : ' , port); 

the model follows(people.js):

var mongoose = require('mongoose'); var schema = mongoose.schema;  var peopleschema = new schema({     name: string,     surname: string });  module.exports = mongoose.model('person',peopleschema) 

my db correctly filled value, , if access using mongo shell (db.people.find()) correct result. instead, using code, doesn't work.

you need specify condition or search criteria object in find() method, it's missing in code. included callback function. try rewriting routing method , changing model name reflect database name as:

people.js ('./models/people'):

var mongoose = require('mongoose'); var schema = mongoose.schema;  var peopleschema = new schema({     name: string,     surname: string }); module.exports = mongoose.model('people', peopleschema); 

or should wish retain model name person, can pass collectionname third argument in model() function arguments:

module.exports = mongoose.model('person', peopleschema, 'people'); 

or property in schema definition:

var peopleschema = new schema({     name: string,     surname: string }, { collection: 'people' });  module.exports = mongoose.model('person', peopleschema); 

server (server.js):

/* if using renamed people model */ router.route('/getpeople').get(function (req,res){     people.find({}, function (err, people) {         console.log('err: ' , err);         console.log('people: ' , people);         if (err)             res.send(err)         res.json(people);     }); }); 

or via executing query:

/* if using renamed people model */ router.route('/getpeople').get(function (req,res){     people.find().exec(function (err, people) {         console.log('err: ' , err);         console.log('people: ' , people);         if (err)             res.send(err)         res.json(people);     }); }); 

Comments