【发布时间】:2023-03-08 04:23:01
【问题描述】:
所以我正在尝试向我的网站添加登录功能,并且我正在使用带有 mognoose 的 typescript,我得到了这个Property 'password' does not exist on type 'Promise<void>' 错误。
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>'。谢谢!
【问题讨论】:
-
getUserFromLogin是一个async函数,它返回一个 promise 的......实际上,未定义的。但是一旦你做出了用户的承诺,你仍然需要在请求处理程序中解决这个问题,使用.then或await。
标签: typescript mongodb express mongoose