2015-11-06 39 views
5

मैं इस ट्यूटोरियल से Node.js जानने के लिए कोशिश कर रहा हूँ नहीं है: https://scotch.io/tutorials/build-a-restful-api-using-node-and-express-4Node.js लेखन त्रुटि: भालू एक समारोह

जब मैं बनाना एक भालू पोस्ट/api के लिए मिलता है/भालू अनुभाग मैं से निम्नलिखित त्रुटि मिलती है डाकिया enter image description here

यह कहता है कि भालू एक समारोह नहीं है और जब मैं इसे तुरंत भालू = नया भालू(); जब मुझे त्रुटि मिलती है।

यहाँ मेरी 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

क्या आपने ब्रेकपॉइंट लगाया और जांच की कि 'भालू' का मूल्य लाइन 47 पर है। Server.js में? – nem035

+0

मुझे पता है कि यह वास्तव में वास्तव में बदसूरत है, लेकिन क्या आप अपनी आवश्यकताओं को मार्ग के अंदर ले जाने की कोशिश कर सकते हैं? '' 'var Bear = आवश्यकता ('./ app/models/bear');' '' '' var bear = new bear(); '' ' मुझे संदेह है कि इसमें कुछ काम करने की आवश्यकता है – Komo

उत्तर

0

क्षमा करें, मैं टिप्पणी नहीं कर सकता। तुम्हारा करने के लिए यहाँ

स्थानीय MongoDB डेटाबेस और सेट कनेक्शन स्ट्रिंग 1.Use:

इस बातों का प्रयास करें

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

डाकिया में 2.Set शरीर पैरामीटर, शीर्षक नहीं।

संबंधित मुद्दे