【发布时间】:2021-02-23 05:20:01
【问题描述】:
我将 MongoDB、Mongoose 与 typescript 结合使用。我的问题如下:
我已经定义了一个这样的模型:
export default conn.model<AdminInterface & Document>('Admin', adminSchema)
export interface AdminInterface {
email: string,
password: string,
role: string,
created: Date,
author: {
name: string,
bio: string,
githubUrl: string,
stackoverflowLink: string,
twitterLink: string,
image: string,
image_webp: string,
},
}
它不会引发错误。
现在我想做一个简单的查询,例如:
import { AdminInterface } from "../model/admin"
export function getAdmin(): Promise<AdminInterface | null> {
return Admin.findOne({ role: 'admin' }, { password: 0 })
}
但它给我抛出了这个错误:
类型“DocumentQuery
”缺少类型“AdminInterface”中的以下属性:电子邮件、密码、角色、已创建、作者
我做错了什么?我如何告诉.findOne 方法我的响应会是什么样子?
【问题讨论】:
-
findOne是异步的,也许您返回的是Promise并且承诺字段与您的界面不匹配? -
@J.F.是的,它返回了我现在尝试过的承诺
Promise<AdminInterface>没有帮助。我试过Promise<any>什么都没有
标签: javascript mongodb typescript mongoose