sails.js - 我在 SailsJs 上使用 waterline-orm 模型方法“.archive()”有一个“UsageError”
问题描述
当我尝试在 Sails 后端存档记录时,这是邮递员返回的错误。
UsageError: 新记录的初始数据无效。\n详细信息:\n 无法使用提供的新记录之一:所需属性缺少值id
。应为字符串,但得到: undefined\n [?] 请参阅https://sailsjs.com/support以获取帮助
请问有人知道这个错误是什么意思吗?提前致谢。
感谢您的回复我的代码: - 在控制器中:
try {
await Laboratory.archive({id: inputs.id});
} catch (error) {
console.log('Error-6 CORE-DELETE_LABORATORY:', error)
exits.failed(`Error-6 CORE-DELETE_LABORATORY: ${error}${inputs.id}`)}
在文件 config/models.js 中:
module.exports.models = {
migrate: 'alter',
fetchRecordsOnUpdate: true,
fetchRecordsOnCreate: true,
fetchRecordsOnCreateEach: true,
attributes: {
createdAt: { type: 'ref', columnType: 'datetime', autoCreatedAt: true, },
updatedAt: { type: 'ref', columnType: 'datetime', autoUpdatedAt: true, },
id: { type: 'string', columnName: '_id' },
},
dataEncryptionKeys: {
default: 'dRYQHBf8Zpza2vMS5BB3qcSiLspJP4BG37I2JkP2yYw='
},
cascadeOnDestroy: true
};
实验室型号:
module.exports = {
attributes: {
type: {
type: 'string',
isIn: ['INSIDE', 'OUTSIDE'],
required: true,
},
raisonSocial: {
type: 'string',
required: true,
unique: true,
},
city: {
type: 'string'
},
address: {
type: 'string'
},
email: {
type: 'string'
},
neighborhood: {
type: 'string'
},
contacts: {
type: 'string'
},
logo: {
type: 'string'
},
lat: {
description: 'Latitude',
type: 'number'
},
long: {
description: 'Longitude',
type: 'number'
},
website: {
type: 'string',
defaultsTo: ''
},
subdomain: {
type: 'string',
unique: true,
required: true,
},
mobileMoney: {
type: 'string'
},
bank: {
type: 'string',
defaultsTo: ''
},
bankAccountID: {
type: 'string',
defaultsTo: ''
},
validated : {
type: 'boolean',
defaultsTo : false
},
f_establishment: {
model: 'establishment'
},
director: {
description: 'Chef of laboratory id',
type: 'json',
example: '{name, phone, role}',
required: true,
}
},
customToJSON () {
if (this.logo) {
this.logo = `${process.env.BASE_URL}/avatar/${this.logo}`
}
return this
}
};
谢谢。
解决方案
UsageError
当我们以不正确的方式使用 db 方法时,Sails 会抛出异常。更准确地说,这就是 Sails 在其文档中的内容。
当错误的名称为:“UsageError”时,这表明 Waterline 方法使用不正确,或使用无效选项执行(例如,尝试创建违反模型的高级验证规则之一的新记录。)
https://sailsjs.com/documentation/concepts/models-and-orm/errors#?usage-errors
举一个简单的例子,如果你有一个带有限制参数的数据库查询,并且你开始为该参数传递字符串值,那么 Sails 就会开始抛出UsageError
。
在您的情况下,从错误细节本身来看,事情应该是不言自明的。您的代码流在尝试运行时正在undefined
运行,但如果您看到.inputs.id
Laboratory.archive({id: inputs.id})
id
config/models.js
只需进行基本调试以查看为什么inputs.id
未定义并解决该问题,您应该一切顺利。
推荐阅读
- google-apps-script - 仅在 Apps 脚本编辑器中运行脚本时出现错误
- gradle - 我可以简单地使用 maven-publish Gradle 插件上传到 Bintray 还是必须使用 Bintray 插件
- c - 复制/指向 typedef 结构的指针
- swift - 查找选择了哪个标签栏项目
- .net-core - 环境变量配置如何在 dotnet core 中工作?
- python - 类似整数的字符串中的隐藏字符
- c# - 如何在 string.replace 上使用字符串插值
- sql - 连接多个用作索引的表 - Laravel
- python - 如何将数字文本文件读入数组数组
- sed - 从一个文件输入并在另一个文件中匹配并打印直到模式匹配