嘿所有, 我對mongodb,mongoose和node.js很新。我想創建一個小演示來看看貓鼬是如何工作的。安裝(和測試的correctes)的node.js我下載貓鼬和後嘗試下面的代碼(也提供了貓鼬網站):設置Mongoose提供演示不工作
require.paths.unshift('vendor/mongoose');
var mongoose = require('mongoose').Mongoose;
mongoose.model('User', {
properties: ['first', 'last', 'age', 'updated_at'],
cast: {
age: Number,
'nested.path': String
},
indexes: ['first'],
setters: {
first: function(v){
return this.v.capitalize();
}
},
getters: {
full_name: function(){
return this.first + ' ' + this.last
}
},
methods: {
save: function(fn){
this.updated_at = new Date();
this.__super__(fn);
}
},
static: {
findOldPeople: function(){
return this.find({age: { '$gt': 70 }});
}
}
});
var db = mongoose.connect('mongodb://localhost/db');
var User = db.model('User');
var u = new User();
u.name = 'John';
u.save(function(){
sys.puts('Saved!');
});
User.find({ name: 'john' }).all(function(array){
});
的問題是,當我運行節點myfile.js我得到了以下錯誤:
node.js:181
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Cannot find module 'mongoose'
at Function._resolveFilename (module.js:320:11)
at Function._load (module.js:266:25)
at require (module.js:364:19)
at Object.<anonymous> (/my/path/to/mongoose+node test/myfile.js:2:16)
at Module._compile (module.js:420:26)
at Object..js (module.js:426:10)
at Module.load (module.js:336:31)
at Function._load (module.js:297:12)
at Array.<anonymous> (module.js:439:10)
at EventEmitter._tickCallback (node.js:173:26)
現在,我要再次說,我真的很新的這一點,所以我稱之爲「貓鼬+節點測試」文件夾,裏面有隻包含了一堆的JavaScript文件的貓鼬文件夾,當然還有myfile.js。我可能錯過了什麼?
它沒有工作:(。 – Masiar 2011-04-23 10:23:10
是的,這將是後面的一步。現在很明顯,你的NPM沒有正確安裝。之後你必須安裝貓鼬,然後來這個。 – neebz 2011-04-23 10:57:52
我做了一切,它仍然不起作用:/。現在不知道該去哪裏了 – Masiar 2011-04-23 11:45:36