2
您好:我是Node/Mongo/Express等的新手,試圖通過貓鼬獲得與mongodb的簡單連接。在這一點上,我只是想建立一個連接,但得到的錯誤:Node.JS/Express/Mongodb/Mongoose簡單連接問題
node.js:134
throw e; // process.nextTick error, or 'error' event on first tick
^
ReferenceError: mongoose is not defined
at Object.<anonymous> (/Users/mattydorey/Code/napkin_0.1/testdb.js:8:1)
at Module._compile (module.js:407:26)
at Object..js (module.js:413:10)
at Module.load (module.js:339:31)
at Function._load (module.js:298:12)
at Array.<anonymous> (module.js:426:10)
at EventEmitter._tickCallback (node.js:126:26)
任何幫助深表感謝。這裏是我的代碼:
// Launch express and server
var express = require('express');
var app = express.createServer();
//connect to DB
var moongoose = require('/Users/me/node_modules/mongoose').Mongoose;
var db = mongoose.connect('mongodb://localhost/napkin_0.1');
//Configure Node w/ Dependencies
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(require("stylus").middleware({
src: __dirname + "/public",
dest: __dirname + "/public",
compress: false
}));
app.use(express.bodyParser());
app.use(express.methodOverride());
//app.use(require('stylus').middleware({ src: __dirname + '/public' }));
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
// Define Model
mongoose.model('User', {
collection: 'user',
properties: [
'created',
'username',
'password',
'email'
],
indexes: [
'created',
[{username: 1},{unique: true}],
[{email: 1},{unique: true}]
],
static: {},
methods: {},
setters: {},
getters: {}
}
);
//Define Collection
var User = db.model('User');
// Handle Data
var user = new User();
user.created = new Date();
user.username = "TEST";
user.password = "PASS";
user.email = "someemail";
user.save();
//Launch Server
app.listen(3002);
改成了要求(「貓鼬」)和貓鼬似乎運行正常。但是我得到一個錯誤,說Object函數Mongoose()沒有方法'connect'。這是否意味着貓鼬出現錯誤? – Matty 2011-06-12 09:09:21
好的,試試看: 'var mongoose = require('mongoose'); var db = mongoose.connect('mongodb://localhost/napkin_0.1');' – Rodolphe 2011-06-12 09:14:17
Works!真棒。是的,看起來像是在新版本中刪除了.Mongoose要求。謝謝你的幫助。 – Matty 2011-06-12 09:21:12