Tony
Tony

Reputation: 1057

Typescript - Why `methods` property doesn't exist in mongoose?

I was coding typescript in nodejs. When I was coding mongoose schema, Typescript compiler telled me as following:

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

I felt odd. I had refer doc in Instance methods section of guide. It mentions as following:

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

I think methods is a available property or API. But for typescript, it's wrong.

Next, I lookup definition. I find method(name: string, fn: Function) and method(method: Object) these properties but it hasn't methods.


In short, you don't answer me why author of mongoose definition doesn't define the property. I need a answer the methods in mongoose is actually available or not?

Upvotes: 2

Views: 856

Answers (3)

Sumit De
Sumit De

Reputation: 176

Today mongoose has released new version of mongoose 5.11.0 . If you have "mongoose": "^5.x.xx" in package.json you will get many errors like this. So, I suggest to change "mongoose": "~5.x.xx" to fix them all.

Upvotes: 0

user5077069
user5077069

Reputation:

methods property does exist in mongoose, but using mongoose methods/statics the way like javascript in typescript will cause error. Here are some workarounds.

workaround A:

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

workaround B :

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

Upvotes: 0

Amid
Amid

Reputation: 22372

No there is no such property as "methods" in pure javascript. Its is a mongoose specifics. Please note that node.js use internally the same google V8 javascript engine as the chrome browser does - so there is no such thing as pure javascript for node.js.

Upvotes: 1

Related Questions