【发布时间】:2021-09-14 06:53:37
【问题描述】:
使用 typescript 界面定义的猫鼬模型如下:
import { Schema, model, Document } from 'mongoose';
export interface IUserInfo extends Document {
userId: string;
email: string;
}
const UserInfoSchema:Schema = new Schema<IUserInfo>({
userId: { type: String, unique: true },
email: String
}, { timestamps: true });
export const UserInfoModel = model<IUserInfo>('userInfo', UserInfoSchema);
使用UserInfoModel通过用户ID获取电子邮件,猫鼬方法如下:
const user: IUserInfo = await UserInfoModel.findOne({ userId: req.query.userId });
在userId 上收到此错误:
Type 'string | ParsedQs | string[] | ParsedQs[]' is not assignable to type 'Condition<string>'.
Type 'string[]' is not assignable to type 'Condition<string>'.
Type 'string[]' is not assignable to type 'string'.
166 const user: IUserInfo = await UserInfoModel.findOne({ userId: req.query.usreId });
~~~~~~
src/models/userInfo.ts:4:3
4 userId: string;
~~~~~~
The expected type comes from property 'userId' which is declared here on type 'FilterQuery<IUserInfo>'
我不知道我在这里做错了什么。
【问题讨论】:
-
您提供的代码运行良好。显示完整的错误堆栈。或者给出一个最小的、可重现的例子
-
@slideshowp2 更新错误堆栈
标签: node.js typescript mongodb mongoose