2017-08-15 3 views
0

私は単純な残りのAPIを構築しようとしています。まず、2つのコレクションの書籍とジャンルがあります。一度私はポストマンを使って投稿しても問題はありませんが、書籍集に投稿すると約2分かかってしまいます(あまりにも多くのことを考えると)、サーバが正常に動作していることを知って接続エラーが返されます。投稿リクエストは1つのコレクションでは動作しますが、別のコレクションでは動作しません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

お知らせ:私はジャンルのリクエストを投稿したら、私は動作しますが、私は本を何のために投稿したら、起こる。

答えて

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); 
    }) 
}); 
関連する問題