node.js - error in json format in mongodb -
var express=require('express'); var app=express(); var bodyparser=require('body-parser'); var mongoose=require('mongoose'); var createerror = require('http-errors') app.use(bodyparser.json()); genre=require('./model/genre') book=require('./model/book') //connect mongoose mongoose.promise = global.promise; mongoose.connect('mongodb://localhost/bookstore', { usemongoclient: true}); var db=mongoose.connection; /* "/api/books" * get: finds books * post: creates new book */ app.get('/api/books',function(req,res){ book.getbook(function(err,book){ if(err){ throw err; //want error in json format } //json response show books in json format res.json(book); }); }); //connection mongodb localhost app.listen(27017); console.log('running on port 27017'); error is: typeerror: book.getbook not function
please tell me how throw error in json format new mongodb..... using visual studio changes mongodb+node.js+express want error should display in json format....
ok here way throw error in json.
app.get('/api/books',function(req,res,callback){ book.getbooks(function(err,book){ if(err){ res.status(404).send({ error: 'error in mongo of kalpita!' }); }else{ //json response show books in json format res.json(book); } },10); });
and add
book.find({},callback).limit(limit);
in model.
Comments
Post a Comment