简体   繁体   English

当我尝试使用 mongoose 方法时出现 Typescript 错误

[英]Typescript error when I try to use mongoose methods

I have a User interface:我有一个用户界面:

import {Document} from "mongoose";

export interface IUser extends Document{
    email: string;
    password: string;
    strategy: string;
    userId: string;
    isValidPassword(password: string): boolean;
}

And a schema:和一个模式:

import { IUser } from "../interfaces/User";

const userSchema = new Schema({
  strategy: {
    type: String,
    enum: ["local", "facebook", "google"]
  },
  email: {
    type: String,
    lowercase: true
  },
  password: {
    type: String
  },
  userId: {
    type: String
  }
});
const User = mongoose.model<IUser>("user", userSchema);

Now when I am trying to use:现在,当我尝试使用时:

const user :IUser= User.findOne({email});
        if(!user){
            done(null, false)
        }
        user.isValidPassword(password);

It says " TS2740: Type 'DocumentQuery<IUser | null, IUser, {}>' is missing the following properties from type 'IUser': email, password, strategy, userId, and 53 more."它说“TS2740:类型'DocumentQuery <IUser | null,IUser,{}>'缺少'IUser'类型的以下属性:email,密码,策略,用户ID和另外53个。”

If I try to remove 'IUser' from const user:IUser= User.findOne({email});如果我尝试从const user:IUser= User.findOne({email}); . . It doesnt let me use user.isValidPassword(password);它不允许我使用user.isValidPassword(password);

And I dont want to use any type for user.而且我不想为用户使用any类型。

You should query the user document using await Model.findOne() statement, see Model.findOne() .您应该使用await Model.findOne()语句查询user文档,请参阅Model.findOne()

It will return the user with the type IUser | null它将返回类型为IUser | nulluser IUser | null . IUser | null

Then, you can call the Instance methods - user.isValidPassword(password) .然后,您可以调用实例方法- user.isValidPassword(password)

An working example:一个工作示例:

user.ts : user.ts

import { Document, Schema, model } from 'mongoose';

interface IUser extends Document {
  email: string;
  password: string;
  strategy: string;
  userId: string;
  isValidPassword(password: string): boolean;
}

const userSchema = new Schema({
  strategy: {
    type: String,
    enum: ['local', 'facebook', 'google'],
  },
  email: {
    type: String,
    lowercase: true,
  },
  password: {
    type: String,
  },
  userId: {
    type: String,
  },
});
const User = model<IUser>('user', userSchema);

export { User, IUser };

main.ts : main.ts

import { IUser, User } from './user';

async function main() {
  const email = 'teresa.teng@gmail.com';
  const password = '123';
  const user: IUser | null = await User.findOne({ email });
  if (!user) {
    return;
  }
  user.isValidPassword(password);
}

暂无
暂无

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

相关问题 当我尝试比较 Mongoose 中的日期时,我收到 ZCCADCDEDB567ABAE643E15DCF0974E503Z 服务器错误,为什么? - When I try comparing dates in Mongoose, I get Mongoose Server Error, why? 我尝试使用Grunt时出现语法错误 - Syntax Error When I Try to Use Grunt 尝试使用 Firebase Auth 时出现错误 - I get an Error when I try to use Firebase Auth 使用push()时出现mongoose错误 - mongoose error when use push() 当我尝试将 return 与三元运算符一起使用但不与 if 语句一起使用时,出现 UnexpectedToken 错误? - UnexpectedToken error when I try to use return with ternary operator but not with if statement? 当我尝试在组件上使用数组方法时,为什么我的数组变量未在我的组件中定义? - Why is my array variable undefined in my component when I try to use array methods on it? 当我尝试将它添加到材质 UI 开关时,React typescript onChange 显示错误 - React typescript onChange is showing error when I try to add it to material UI switch Mongoose:增加我的文档版本号不起作用,我在尝试保存时遇到版本错误 - Mongoose: Incrementing my documents version number doesn't work, and I'm getting a Version Error when I try to save ZCCADCDEDB567ABAE643E15DCF0974E503Z 架构在我尝试保存时不起作用 - Mongoose schema does not work when I try to save it 猫鼬模型,为什么我需要使用“ new”? (TypeScript) - mongoose model, why do I need to use “new”? (TypeScript)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM