【问题标题】:Node.js TypeError: Bear is not a functionNode.js TypeError:Bear 不是函数
【发布时间】:2016-02-07 19:16:06
【问题描述】:

我正在尝试从本教程中学习 node.js:https://scotch.io/tutorials/build-a-restful-api-using-node-and-express-4

当我进入创建 Bear POST /api/bears 部分时,我从邮递员那里收到以下错误

它说 Bear 不是一个函数,当我实例化它时 var bear = new Bear(); 是我得到错误的时候。

这是我的bear.js

// app/models/bear.js

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

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

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

这是我的server.js

// server.js

// BASE SETUP
// =============================================================================

// call the packages we need
var express    = require('express');        // call express
var app        = express();                 // define our app using express
var bodyParser = require('body-parser');
var mongoose   = require('mongoose');
mongoose.connect('mongodb://node:node@novus.modulusmongo.net:27017/Iganiq8o'); // connect to our database
var Bear     = require('./app/models/bear');

// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

var port = process.env.PORT || 8080;        // set our port

// ROUTES FOR OUR API
// =============================================================================
var router = express.Router();              // get an instance of the express Router

// middleware to use for all requests
router.use(function(req, res, next) {
    // do logging
    console.log('Something is happening.');
    next(); // make sure we go to the next routes and don't stop here
});

// test route to make sure everything is working (accessed at GET http://localhost:8080/api)
router.get('/', function(req, res) {
    res.json({ message: 'hooray! welcome to our api!' });   
}); 
// <-- route middleware and first route are here

// more routes for our API will happen here

// on routes that end in /bears
// ----------------------------------------------------
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!' });
        });
        
    });


// REGISTER OUR ROUTES -------------------------------
// all of our routes will be prefixed with /api
app.use('/api', router);

// START THE SERVER
// =============================================================================
app.listen(port);
console.log('Magic happens on port ' + port);

我可以做些什么来让熊成为一个功能?

【问题讨论】:

  • 你是否设置了断点并检查了 server.js 中第 47 行 Bear 的值?
  • 我知道它真的很难看,但你可以尝试将你的需求移动到路线内部吗? var Bear = require('./app/models/bear'); var bear = new Bear(); 我怀疑这与 require 的工作方式有关

标签: node.js postman


【解决方案1】:

抱歉,我无法发表评论。

试试这个:

1.使用本地 mongodb 数据库,并将此处的连接字符串设置为您的:

    mongoose.connect('mongodb://node:node@novus.modulusmongo.net:27017/Iganiq8o');

2.在Postman中设置body参数,而不是header。

【讨论】:

    猜你喜欢
    • 2018-03-19
    • 2018-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-10
    • 2016-06-16
    • 1970-01-01
    相关资源
    最近更新 更多