2015-11-06 62 views
5

我试图从本教程学习的node.js:https://scotch.io/tutorials/build-a-restful-api-using-node-and-express-4Node.js的类型错误:熊不是一个函数

当我到了创建熊POST/API /熊节我收到以下错误邮差 enter image description here

它说熊是不是一个函数,当我实例化它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:[email protected]: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); 

我能做些什么,使承担的功能?提前致谢。

+0

你有没有放置一个断点,并检查'​​Bear'的值是在第47行在server.js? – nem035

+0

我知道这真的很难看,但你可以尝试将你的要求移动到路线内吗?我怀疑它与需求的工作方式有关系,如果需要('./ app/models/bear');''' '''var bear = new Bear();''' – Komo

回答

0

对不起,我不能评论。这里

1.使用本地的MongoDB数据库并设置连接字符串到你:

试试这个东西邮差

mongoose.connect('mongodb://node:[email protected]:27017/Iganiq8o'); 

2.设置参数身上,而不是头部。

相关问题