获取错误:表达式是不可调用的Mongoose NextJS



我试图将数据保存到NextJS页面上的猫鼬模型。我在这一行得到一个打字错误:

await User.create(data)

该表达式不可调用。联合类型"{,"_id"|"__v"| "id" "|"__v"|"id&quot祝辞祝辞祝辞(医生:Z): Promise<……在;& lt; Z =Document<……比;| _AllowStringsForIds<……在祝辞(文档:Z[],选择吗?:SaveOptions): Promise<……祝辞;;Z = Document<…>|_AllowStringsForIds<……在祝辞(…docs: Z[]): promise…'有签名,但这些签名都不兼容。

代码工作-我可以看到添加到数据库中的数据,但是

  1. 在heroku上部署失败,出现同样的错误
  2. 我想清除这个错误在我的VSCode[问题]标签以及(烦人)

下面是我从User模型导出的内容:

export default  (mongoose.models.User || mongoose.model('User', UserSchema));

虽然年纪有点大,但我的见解在这里:

我有同样的问题,发现我可以通过显式地给mongoose.models.User与模式使用相同的类型来使它没有@ts-ignore而工作。

export interface User {
username: string;
password: string;
}
const schema = new mongoose.Schema<User>(
{
username: {
type: String,
required: true,
},
password: {
type: String,
required: true,
},
}
);

export default (mongoose.models.User as mongoose.Model<User>) || mongoose.model('User', schema);

最新更新