mongodb中的json格式错误

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 to Mongoose mongoose.Promise = global.Promise; mongoose.connect('mongodb://localhost/bookstore', { useMongoClient: true}); var db=mongoose.connection; /* "/api/books" * GET: finds all books * POST: creates a new book */ app.get('/api/books',function(req,res){ Book.getBook(function(err,book){ if(err){ throw err; //Want this error in json format } //JSON response will show all books in JSON format res.json(book); }); }); //Connection to the mongodb localhost app.listen(27017); console.log('Running on port 27017'); error is: TypeError: Book.getBook is not a function 

请告诉我如何在json格式中抛出错误,因为我对mongodb是新的…..我正在使用visual studio的变化与该mongodb + node.js + Express Want错误应显示在JSON格式….

       

网上收集的解决方案 "mongodb中的json格式错误"

好的,这里是在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 will show all books in JSON format res.json(book); } },10); }); 

并添加

Book.find({},callback).limit(极限);

在模型中。