2011-09-16 13 views
1

これは私のlocationsModel.jsファイルです:私のコントローラで私のマングースモデルはなぜロードされませんか?

var LocationSchema, LocationsSchema, ObjectId, Schema, mongoose; 
mongoose = require('mongoose'); 
Schema = mongoose.Schema; 
ObjectId = Schema.ObjectId; 
LocationSchema = { 
    latitude: String, 
    longitude: String, 
    locationText: String 
}; 
LocationsSchema = new Schema(LocationSchema); 
LocationsSchema.method({ 
    getLocation: function(callback) { 
    return console.log('hi'); 
    } 
}); 
exports.Locations = mongoose.model('Locations', LocationsSchema, 'locations'); 

、私が持っている:

var Locations, mongoose; 
mongoose = require('mongoose'); 
Locations = require('../models/locationsModel').Locations; 
exports.search = function(req, res) { 
    var itemText, locationText; 
    Locations.getLocation('info', function(err, callback) { 
    return console.log('calleback'); 
    }); 
    return; 
}; 

私はそれを実行すると、私は次のエラーを取得する:

TypeError: Object function model() { 
    Model.apply(this, arguments); 
    } has no method 'getLocation' 

私は何行方不明?

+0

;'あなたは、単に行くことができます'Locations = mongoose.model( 'Locations')' –

答えて

3

私はあなたが後にしているのは方法ではなく静的なものだと思います。

docsを1として:私はあなたが文字列パラメータだけでなく、コールバックを持っているgetLocationsの使用を見て(次のようにgetLocations関数を定義するべきだと思い

LocationsSchema.statics.getLocation = function(param, callback) { 
    return console.log('hi'); 
} 

編集:

staticsmethodsの違いは、あなたがそのタイプの "タイプ"または "オブジェクト"を呼び出すかどうかです。 examples:あなたのような呼び出したい

BlogPostSchema.methods.findCreator = function (callback) { 
    return this.db.model('Person').findById(this.creator, callback); 
} 

は:。

場所=は( '../モデル/ locationsModel')場所を必要とする代わりに、 `のあなたのコントローラで
BlogPost.findById(myId, function (err, post) { 
    if (!err) { 
    post.findCreator(function(err, person) { 
     // do something with the creator 
    } 
    } 
}); 
+0

ありがとうございます!うわ〜すごい!だから、いつ私は 'static'と' method'を使うのですか? – Shamoon

+0

@Shamoon:私の答えを編集しました。 – beny23

関連する問題