javascript - 如何修复 Typescript 无法确定类型错误
问题描述
这是我的代码 Nestjs mongoose test.schema.ts
@Schema({ timestamps: true })
export class Tests {
@Prop({ required: true, index: true })
testid: number;
@Prop({ required: false })
extra_id: string | number;
@Prop({ required: false })
points: number;
}
为此,我遇到了错误,CannotDetermineTypeError:无法确定“Tests.extra_id”字段的类型(使用了联合/交集/模糊类型)。确保您的财产用“ @Prop({ type: TYPE_HERE })
”装饰器装饰。如何解决这个问题。
解决方案
您不能声明动态类型 Like
@Prop({ required: false })
extra_id: string | number;
Mongoose 只支持一种类型
@Prop({ type : String, required: false })
extra_id: string;
推荐阅读
- magento - CMS页面面临缓存问题
- swift - 如何清除uitableview中的选择并动态显示?
- ajax - AJAX - 在 laravel 上使用 ajax 上传图片而没有表单
- reactjs - 卡在侧边栏的反应路由上
- javascript - 了解 Meteor 中的 withtracker() 组件
- javascript - Kendo TabStrip 模板内部在 Kendo Grid 中不起作用
- c# - ToListAsync() 在 ASP NET CORE 上失败,但 ToList() 有效 - 错误“序列不包含元素”。
- node.js - 连接到 mongoDB
- reactjs - React 中的渲染顺序
- database - 错误的密码/电子邮件错误身份验证活动 - 使用 SQLite