2016-02-13 79 views
2

我在nodejs中編寫打字稿。當我編碼貓鼬模式時,Typescript編譯器如下所示:Typescript - 爲什麼'方法'屬性不存在於貓鼬中?

app/models/user.schema.ts(15,12): error TS2339: Property 'methods' does not exis 
t on type 'Schema'. 

我覺得很奇怪。我已經在guideInstance methods部分提及doc。它提到如下:

// assign a function to the "methods" object of our animalSchema 
animalSchema.methods.findSimilarTypes = function (cb) { 
    return this.model('Animal').find({ type: this.type }, cb); 
} 

我認爲methods是一個可用的屬性或API。但對於打字稿來說,這是錯誤的。

接下來,我查找的定義。我覺得method(name: string, fn: Function)method(method: Object)這些屬性,但它並沒有methods


總之,你不回答我爲什麼貓鼬定義的作者沒有定義屬性。我需要一個答案methodsmongoose實際上是否可用?

回答

1

沒有有沒有這樣的特性,以純JavaScript「方法」。它是一種貓鼬細節。請注意,node.js在內部使用與Chrome瀏覽器相同的Google V8 JavaScript引擎 - 因此,不存在像純節點jj這樣的東西。

1

methods屬性不存在於貓鼬,但使用方法貓鼬/靜像打字稿JavaScript的方式會導致錯誤。以下是一些解決方法。

替代方法:

userSchema['methods'].findSimilarTypes = function (cb) { 
    return this.model('Animal').find({ type: this.type }, cb); 
} 

變通方法B:

userSchema.method('findSimilarTypes', function (cb) { 
    return this.model('Animal').find({ type: this.type }, cb); 
})