【发布时间】:2019-06-20 17:07:50
【问题描述】:
我想用我自己的类和函数来扩展已安装的模块(Mongoose)。 我编写了函数和类,它们工作正常。
现在我想将它们添加到mongoose 模块。
所以我现在拥有的是这个文件:
mongoInstance.ts
import * as mg from 'mongoose'
class _Schema extends mg.Schema {
constructor(definition?: mg.SchemaDefinition, options?: mg.SchemaOptions) {
super(definition, options)
this.addData()
}
private addData() {
//do stuff
}
}
function _limitedRequest(schema: mg.Schema, options = 200) {
schema.pre("find", function (next) {
this.limit(options)
next()
})
}
declare module 'mongoose' {
export class _Schema extends Schema {
constructor(definition?: SchemaDefinition, options?: SchemaOptions)
private addData(): void
}
export function _limitedRequest(schema: Schema, options?: number): void
}
现在我可以在我的应用程序的任何地方做:
otherFile.ts
import * as mg from 'mongoose'
//doesnt work
var test1= new mg._Schema({})
//works
var test2= new mg.Schema({})
所以 VSCode 向我推荐了我的课程,而 IntellIsense 有效。但似乎没有该类的实现。
将我的代码编译为 bundle.js 时,Webpack 不会抛出任何错误,但是当我尝试使用 node bundle.js 运行我的 bundle.js 时,它会显示:TypeError: mg._Schema is not a constructor p>
【问题讨论】:
标签: node.js typescript mongoose