javascript - Error with Schema - ReferenceError: Phone is not defined -
i'm having small problem when building crud api node.js , express.
when post api "referenceerror: phone not defined"
// server.js
// base setup // =============================================================================
// call packages need
var express = require('express'); // call express var app = express(); // define our app using express var bodyparser = require('body-parser'); var phone = require('./models/phone'); var mongoose = require('mongoose'); mongoose.connect('mongodb://<userid>:<pass>@apollo.modulusmongo.net:27017/ugygy5qe'); // configure app use bodyparser() // let data post app.use(bodyparser.urlencoded({ extended: true })); app.use(bodyparser.json()); var port = process.env.port || 8080; // set our port // routes our api // ============================================================================= var router = express.router(); // instance of express router router.use(function(req,res,next){ console.log('something happening'); next(); }); router.route('/phones').post(function(req, res){ var phone = new phone(); phone.name = req.body.name; phone.save(function(err){ if(err){ res.send(err); } res.json({message: 'phone create'}); }); }); // test route make sure working (accessed @ http://localhost:8080/api) router.get('/', function(req, res) { res.json({ message: 'hooray! welcome our api!' }); }); // more routes our api happen here // register our routes ------------------------------- // of our routes prefixed /api app.use('/api', router); // start server // ============================================================================= app.listen(port); console.log('magic happens on port ' + port);
and here schema file.
var mongoose = require('mongoose'); var schema = mongoose.schema; var phoneschema = new schema({ name: string }); module.exports = mongoose.model('phone', phoneschema);
i'm not sure error is. i've consulted mongoose docs, search around still can't work.
just typo ! check line no 4.
var phone = require('./models/phone');
Comments
Post a Comment