简体   繁体   中英

Class don't understand Mongoose Model using typescript

Is giving a type error, he does not understand the find inside the UserInterface, this error: Property 'find' does not exist on type 'UserInterface'


import User, { UserInterface } from '../schemas/User';

class UserController {
  public user: UserInterface;

  constructor() {
    this.user = new User();
  }

  public async index(req: Request, res: Response): Promise<Response> {
    const users = await this.user.find();
    return res.json(users);
  }
}

export default new UserController();

export interface UserInterface extends Document {
  email?: string;
  firstName?: string;
  lastName?: string;
}

const UserSchema = new Schema(
  {
    email: String,
    firstName: String,
    lastName: String,
  },
  {
    timestamps: true,
  },
);

export default model<UserInterface>('User', UserSchema);

Instead of calling

const users = await this.user.find();

Try calling

const users = await User.find();

As it looks like you are trying to find all users.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM