类型 'Promise<void>' 上不存在属性 'password'

时间:2021-07-13 19:29:58

标签: typescript mongodb express mongoose

所以我正在尝试向我的网站添加登录功能,并且我正在使用带有 mognoose 的 typescript 并且我收到此 Property 'password' does not exist on type 'Promise<void>' 错误 enter image description here

getUserFromLogin

const getUserFromLogin = async (email: string) => {
    await mongo().then(async mongoose => {
        try {
            const res = await UserModel.findOne({
                email,
            });

            return res;
        } finally {
            mongoose.connection.close();
        }
    });
};

用户架构:

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

export interface IUser extends Document {
    id: string;
    username: string;
    email: string;
    password: string;
}

interface reqStringInterface {
    type: StringConstructor;
    required: boolean;
}

const reqString: reqStringInterface = {
    type: String,
    required: true,
};

const userSchema = new Schema<IUser>({
    id: reqString,
    username: reqString,
    email: reqString,
    password: reqString,
});

export const UserModel = model<IUser>("User", userSchema);

我很确定修复方法是在 getUserFromLogin 函数中添加一个返回值,但是当我添加 Promise<any> 时它会抛出此错误 Property 'password' does not exist on type 'Promise<any>'。谢谢!

1 个答案:

答案 0 :(得分:1)

问题在于 constraint forall(i in OPTS,j in CONDS)( result2[i,j] = sum(k in result)(% tried all sorts of things here with i,j, and k and if statements) ); 不返回任何内容,因为您的 getUserFromLogin 位于箭头函数内。为了避免这种混淆,尽量不要在一个方法中混合使用 return resthen 语法,你可以像这样重写:

await

然后你需要等待承诺

const getUserFromLogin = async (email: string) => {
    const mongoose = await mongo();
    try {
      const res = await UserModel.findOne({email});
      return res;
    } finally {
      mongoose.connection.close();
    }
};