简体   繁体   English

班级不理解使用打字稿的猫鼬模型

[英]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'给出了一个类型错误,他不明白 UserInterface 里面的 find,这个错误: Property 'find' 在类型 '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.看起来您正在尝试查找所有用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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