node.js - Post request work with one collection but not with the another using mongodb nodejs? -


i trying build simple rest api. firstly, have 2 collections books , genres. once post using postman in genres post no problems once post books collection post requesting time take 2 minutes(which consider much) return connection error knowing server remain working.

app

// tools used in web development var express = require('express'); var app = express(); var bodyparser = require('body-parser'); var mongoose = require('mongoose');  app.use(bodyparser.json());   genre = require('./models/genre.js'); book = require('./models/book.js');    let conn = mongoose.connection; conn.openuri('mongodb://localhost/bookstore');  conn.on('error', err => console.error('mongodb connection error',  err)); conn.on('connected', () => console.info(`connected mongodb`)); conn.on('disconnected', () => console.info('disconnected  mongodb'));   // routing specific pages: app.get('/', function(req, res){ res.send('hello world'); });  app.get('/api/genres', function(req , res){ genre.getgenres(function(err, genres){     if(err){         throw err;     }     res.json(genres); }) });  app.get('/api/books', function(req , res){ book.getbooks(function(err, books){     if(err){         throw err;     }     res.json(books); }) });  app.get('/api/books/:_id', function(req , res){ book.getbookbyid(req.params._id, function(err, book){     if(err){         throw err;     }     res.json(book); }) });  app.post('/api/genres', function(req , res){ var genre = req.body; genre.addgenre(genre, function(err, genre){     if(err){         throw err;     }     res.json(genre); }) });  app.post('/api/books', function(req , res){ var book = req.body; book.addbook(function(err, book){     if(err){         throw err;     }     res.json(book); }) });   //specify listening port app.listen(3666); //display url on termianl console.log('server running on http://localhost:3666'); 

book

var mongoose = require('mongoose');  //book schema var bookschema = mongoose.schema({  title:{     type: string,     requires: true }, genre:{     type: string,     required: true }, description:{     type: string }, author:{     type: string,     required: true }, publisher:{     type: string,     required: true }, create_date:{     type: date,     default: date.now } });  var book = module.exports = mongoose.model('book', bookschema);  module.exports.getbooks = function(callback, limit){ book.find(callback).limit(limit);  }   module.exports.getbookbyid = function(id, callback){ book.findbyid(id, callback);  }  //add genre module.exports.addbook = function(book, callback){ book.create(book, callback); } 

and the postman request

and database

notice: once post request in genres works once post book nothing happen.

your addbook function takes 2 parameters: book object , callback

//add genre module.exports.addbook = function(book, callback){     book.create(book, callback); } 

but, while calling it, have passed callback. can try use below code:

app.post('/api/books', function(req , res){     var book = req.body;     book.addbook(book, function(err, book){       if(err){          throw err;       }       res.json(book);     }) }); 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -