我試圖使用貓鼬將一些數據發送到數據庫。這是我的代碼到目前爲止。Nodejs Mongoose - 未定義模型
server.js
var express = require('express');
var wine = require('./routes/wines');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var app = express();
app.use(bodyParser.urlencoded({ extended: true}));
app.use(bodyParser.json());
app.get('/wines', wine.findAll);
app.get('/wines/:id', wine.findById);
app.listen(3000);
console.log('Listening on port 3000...');
wine.js(內部模型文件夾)
var mongoose = require('mongoose');
var db = mongoose.connection;
var wineSchema = new mongoose.Schema({
name: String,
description: String
});
var Wine = mongoose.model('Wine', wineSchema);
module.exports = Wine;
wines.js(路徑文件夾中)
exports.addWine = function(req, res) {
// Problem not defined here
var silence = new Wine({ name: 'Silence', description:"cena" })
console.log(silence.name) // 'Silence'
// add it to the database
};
我不斷收到這個錯誤,我不知道爲什麼。 ReferenceError: Wine is not defined
我已經在wine.js(模型)中導出Wine,我不應該能夠在任何地方使用它嗎?
預先感謝您!
內wines.js,你仍然需要要求您要使用的架構。 – 2015-04-02 19:41:18
@Let_IT_roll你檢查了建議的答案嗎? – 2015-04-08 19:09:37