2017-08-15 44 views
0

我正在嘗試構建一個簡單的休息API。首先,我有2本藏書和流派。一旦我使用郵遞員在流派發布沒有問題,但一旦我張貼到書籍收集後的請求時間大約2分鐘(其中考慮太多),然後它返回連接錯誤,知道服務器仍在工作。使用mongodb nodejs發佈請求使用一個集合但不與另一個集合一起工作?

應用

// Tools to be used in the 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 to mongodb`)); 
conn.on('disconnected',() => console.info('Disconnected from 
mongodb')); 


// Routing to 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 the listening port 
app.listen(3666); 
//Display the url on the termianl 
console.log('Server Running On http://localhost:3666'); 

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); 
} 

the postman request

database

注意:一旦我POST請求的類型我的作品,但一旦我上傳的書沒有發生。

回答

0

addBook函數有兩個參數:一個book對象和回調

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

但是,同時調用它,你只有通過回調。您可以嘗試使用以下代碼:

app.post('/api/books', function(req , res){ 
    var book = req.body; 
    Book.addBook(book, function(err, book){ 
     if(err){ 
     throw err; 
     } 
     res.json(book); 
    }) 
}); 
相關問題