【问题标题】:Post request work with one collection but not with the another using mongodb nodejs?使用 mongodb nodejs 发布请求与一个集合一起工作,但不与另一个集合一起工作?
【发布时间】:2017-08-15 19:28:30
【问题描述】:

我正在尝试构建一个简单的 rest api。首先,我有 2 个收藏书籍和流派。一旦我在流派中使用邮递员发布它没有问题,但是一旦我发布到书籍集合中,帖子请求时间大约需要 2 分钟(考虑太多)然后它返回连接错误并知道服务器仍在工作。

应用程序

// 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

注意:一旦我以我工作的类型发布请求,但一旦我为这本书发布请求,什么都没有发生。

【问题讨论】:

    标签: node.js mongodb express postman


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-14
      • 1970-01-01
      • 2018-07-19
      相关资源
      最近更新 更多