【发布时间】:2021-08-02 19:41:38
【问题描述】:
所以我有这个猫鼬模型如下:
const userSchema: mongoose.Schema = new mongoose.Schema(
{
_id: String,
email: {
type: String,
required: true,
},
firstName: String,
lastName: String,
phoneNumber: String,
cmt: {
type: String,
ref: 'Cmt',
},
},
);
您可以看到 cmt 字段指向另一个名为 Cmt 的模型,我将使用它的详细信息 populate
现在在另一个实例上,我需要传入 cmt id 以将其与 userSchema 链接
但那时我会收到"Type 'string' is not assignable to type 'ICmt'." 的打字稿错误
ICmt是Cmt的接口定义。
用户模式界面如下。
export interface IUser {
_id: string;
email: string;
firstName: string;
lastName: string;
phoneNumber: string;
cmt: ICmt;
createdAt?: Date;
updatedAt?: Date;
}
如何在不搞乱填充查询和创建查询的情况下修复此错误?
【问题讨论】:
标签: typescript mongoose