在猫鼬模式中,我使用以下库:https://github.com/VassilisPallas/mongoose-fuzzy-searching
UserSchema.plugin(mongoose_fuzzy_searching, { fields: ['name'] });
然后在服务中,我像这样使用它:
export function fuzzySearchUser(name: string): Query<Array<IUser>>{
return User.fuzzySearch(name)
}
现在在上面的返回行中出现以下Typescript错误:
Property 'fuzzySearch' does not exist on type 'Model<IUser, {}>'.ts(2339)
如果我将它作为具有类型函数的属性添加到IUser界面,也不能将其添加到架构,则没有帮助。
(接口IUser扩展了mongoose.Document)
答案 0 :(得分:1)
您需要创建一个键入文件,因为此库不提供键入内容。试试看。
typings/mongoose-fuzzy-search
declare module 'mongoose-fuzzy-search' {
import { Document, DocumentQuery, Model, Schema } from 'mongoose'
export interface MongooseFuzzyOptions<T> {
fields: (T extends Object ? keyof T : string)[]
}
export interface MongooseFuzzyModel<T extends Document, QueryHelpers = {}>
extends Model<T, QueryHelpers> {
fuzzySearch(
search: String,
callBack?: (err: any, data: Model<T, QueryHelpers>[]) => void
): DocumentQuery<T[], T, QueryHelpers>
}
function fuzzyPlugin<T>(schema: Schema<T>, options: MongooseFuzzyOptions<T>): void
export default fuzzyPlugin
}
然后使用MogooseFuzzyModel声明模型:
export const User = mongoose.model<UserModel>('User', UserSchema) as MongooseFuzzyModel<UserModel>