0

こんにちは、私はmongodb、mongoose、およびnode.jsを初めて使用します。マングースがどのように機能するかを確認するための小さなデモを作成したいと思います。node.js をインストール (および修正内容をテスト) した後、mongoose をダウンロードし、次のコードを試しました (mongoose の Web サイトでも提供されています)。

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){

});

問題は、node 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)

さて、私はこれに本当に慣れていないことをもう一度言わなければならないので、「mongoose + node test」と呼ばれる私のフォルダーには、JavaScriptファイルの束を含むmongooseフォルダーと、もちろんmyfile.jsだけがあります。私はおそらく何かを逃していますか?

4

2 に答える 2

3

マングースが見つかりません。これに対処する最も簡単な方法は、 からインストールすることnpmです。

npm をインストールするには:

curl http://npmjs.org/install.sh | sh

マングースをインストールするには:

npm install mongoose

また、mongoDB をダウンロードしてインストールし、mongoDB サーバーを起動する必要があります。

これunix quickstartは、mongoDB のインストール、実行、およびテストに役立ちます。

あなたの主な問題は、require.paths編集してはならないことです。URL を直接要求するか、パッケージ システムを経由する必要があります。nodejsのドキュメントでは、require.paths避けるべきだと述べています。

npm個人的には、これはデファクタリングの標準であるため、固執することをお勧めします。

于 2011-04-23T10:03:24.947 に答える
1

新しいバージョンでは、使用する必要はありません.Mongoose

以下を置き換えるだけです:

var mongoose = require('mongoose').Mongoose;

と:

var mongoose = require('mongoose')

于 2011-04-23T10:10:59.063 に答える