Mongoose保存回调未被触发

时间:2016-01-03 16:08:51

标签: node.js

bear.js:

var mongoose     = require('mongoose');
var Schema       = mongoose.Schema;

var BearSchema   = new Schema({
    name: String
});

module.exports = mongoose.model('Bear', BearSchema);

server.js:

mongoose.connect('mongodb://myuser:mypass@ds037175.mongolab.com:27175/words');

var Bear = require('./app/models/bear.js');
// ROUTES FOR OUR API
// =============================================================================
var router = express.Router();              // get an instance of the express Router

router.use(function(req,res,next) {
    //do logging
    console.log('Smth is happening');
    next();
});

router.route('/bears')

    // create a bear (accessed at POST http://localhost:8080/api/bears)
    .post(function(req, res) {
        var bear = new Bear();      // create a new instance of the Bear model
        bear.name = req.body.name;  // set the bears name (comes from the request)

        // save the bear and check for errors
        bear.save(function(err) {
            if (err) {
                res.send(err);
              }
            res.json({ message: 'Bear created!' });
        });

    });

我试图与Postman联系,似乎保存回调没有被触发,没有回应 - “一段时间后无法得到任何回应。”

(我试过通过终端连接到我的mongo DB并成功)。 我是node.js的新手,所以对我来说很软。不确定如何调试或为什么会发生这种情况。

0 个答案:

没有答案