2015-11-06 25 views
5

Sto cercando di imparare node.js da questo tutorial: https://scotch.io/tutorials/build-a-restful-api-using-node-and-express-4Node.js TypeError: Orso non è una funzione

Quando arrivo alla Creazione di un orso POST/api/porta sezione ottengo il seguente errore da postino enter image description here

Si dice che l'orso non è una funzione e quando lo istanziamo var bear = new Bear(); è quando ottengo l'errore.

Ecco il mio 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); 

Ed ecco la mia 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); 

Cosa posso fare per rendere sopportare una funzione ?? Grazie in anticipo.

+0

Hai inserito un punto di interruzione e controllato qual è il valore di "Bear" alla riga 47. in server.js? – nem035

+0

So che è davvero brutto, ma puoi provare a spostare il tuo bisogno all'interno del percorso? '' 'var Bear = require ('./ app/models/bear');' '' '' 'var bear = new Bear();' '' Sospetto che abbia qualcosa a che fare con quanto richiede – Komo

risposta

0

Siamo spiacenti, non posso commentare.

Prova queste cose:

1.Utilizzare del database MongoDB locale e stringa di connessione impostato qui al tuo:

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

parametro corpo 2.Set in Postman, non intestazione.

Problemi correlati