找不到Mongoose Model Model Expreesjs4

时间:2014-06-10 15:33:13

标签: express mongoose mlab

我正在尝试遵循教程的模型示例,但在尝试导出模型模块时遇到了问题。它表示我的架构未定义,但架构变量在文件中声明。此问题是否与server.js文件中的mongoose数据库连接更相关?任何帮助都会很棒。感谢。

错误:

/Users/user/Desktop/Projects/node/beerlocker/models/beer.js:9

module.exports = mongoose.model('Beer', BeerSchema);

                                        ^

ReferenceError: BeerSchema is not defined

    at Object.<anonymous> (/Users/user/Desktop/Projects/node/beerlocker/models/beer.js:9:41)

    at Module._compile (module.js:456:26)

    at Object.Module._extensions..js (module.js:474:10)

    at Module.load (module.js:356:32)

    at Function.Module._load (module.js:312:12)

    at Module.require (module.js:364:17)

    at require (module.js:380:17)

    at Object.<anonymous> (/Users/user/Desktop/Projects/node/beerlocker/server.js:6:12)

    at Module._compile (module.js:456:26)

    at Object.Module._extensions..js (module.js:474:10)

beer.js

 var mongoose = require('mongoose');

var BeerShcema = new mongoose.Schema({
    name: String,
    type: String,
    quantity: Number
});

module.exports = mongoose.model('Beer', BeerSchema);

server.js

// Get the packages we need
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var Beer = require('./models/beer');


mongoose.connect('mongodb://user:pw@ds04xxxx.mongolab.com:4xxxx/beerlocker')

// Create our Express application
var app = express();


app.use(bodyParser());

// Use environment defined port or 3000
var port = process.env.PORT || 3000;

// Create our Express router
var router = express.Router();

// Initial dummy route for testing
// http://localhost:3000/api
router.get('/', function(req, res) {
  res.json({ message: 'You are running dangerously low on beer!' });
});

var beersRoute = router.route('/beers');

beersRoute.post(function(req, res) {
    var beer = new Beer();

    beer.name = req.body.name;
    beer.type = req.body.type;
    beer.quantity = req.body.quantity;

    beer.save(function(err){
        if (err)
            res.send(err);
        res.json({ message: 'Beer was added'});
    });
});

// Register all our routes with /api
app.use('/api', router);

// Start the server
app.listen(port);
console.log('Insert beer on port ' + port);

0 个答案:

没有答案