I'm trying to create a custom class decorator, that creates an instance of a repository class, the snippet below represents the "decorator" and the "repository".
import UserModel, { User } from "../models/user.model";
import AbstractRepository from "./abstract.repository";
import { Document, Model } from 'mongoose'
import { injectable } from "inversify";
export function Repository(service: Document): ClassDecorator {
return function (target: Function) {
}
}
@Repository(User)
export class TestRepository {
}
and this other snippet represents my mongoose models
import moongose, { Document, Schema } from 'mongoose'
export interface User extends Document {
name: String,
}
const UserSchema = new Schema<User>({
name: {
type: String
},
})
export default moongose.model<User>("user", UserSchema)
But when I try to use my custom decorator, typescript complains about something and gives this error.
'User' only refers to a type, but is being used as a value here.ts(2693)
Someone already had this issue, and can help me?
The argument of the decorator function Repository(service: Document) {}
is an instance of document but you passed a type User
.
@Repository(User)
should be @Repository(new UserModel)
.
Or change the type of the argument
function Repository(service: Model<User>) {}
then you can call the decorator like this @Repository(UserModel)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.