环回 2 - 在模型上定义对象数组
loopback 2 - defining an array of objects on a model
如果我有一个 usermodel
并且我定义:
"events": {
"type": [
"Object"
]
},
我是否需要在 usermodel.js
中定义任何其他内容才能 post 诸如:[{name: 'sample', ...}, ...]
到用户 table 的事件列?
我问是因为如果我从 .json
中删除这个特定定义,应用程序会编译并且数据库会迁移,但是有了它,应用程序会编译但数据库指出用户存在问题 findByid
。我的调试已将其缩小到这组特定的代码。
我想你可以简单地使用这个结构
{
"events":{
"type": [
{
"key": "type",
"key2": "type"
}
]
}
}
您可以查看 .js 示例 here and .json example here。
但我也可以看到 here 的实现问题
this model has problem. When we fetch the data with any get call, it
renders this particular field as ["Object Object"] even though the
data is properly saved in the database.
我建议您自己尝试,因为它在很大程度上取决于版本和驱动程序。
虽然我想问一下您使用的是哪种数据库?
另一种方法是在数组中将您想要的对象定义为模型,然后将该模型用作您的类型:
型号:Class
{
"name": "Class",
"base": "Model",
"strict": true,
"idInjection": false,
"properties": {
"label": {
"type": "string",
"required": true
}
}
}
型号:学生
{
"name": "Student",
"base": "PersistedModel",
"strict": true,
"idInjection": true,
"properties": {
"classes": {
"type": ["Class"],
"required": false
},
}
}
如果我有一个 usermodel
并且我定义:
"events": {
"type": [
"Object"
]
},
我是否需要在 usermodel.js
中定义任何其他内容才能 post 诸如:[{name: 'sample', ...}, ...]
到用户 table 的事件列?
我问是因为如果我从 .json
中删除这个特定定义,应用程序会编译并且数据库会迁移,但是有了它,应用程序会编译但数据库指出用户存在问题 findByid
。我的调试已将其缩小到这组特定的代码。
我想你可以简单地使用这个结构
{
"events":{
"type": [
{
"key": "type",
"key2": "type"
}
]
}
}
您可以查看 .js 示例 here and .json example here。 但我也可以看到 here 的实现问题
this model has problem. When we fetch the data with any get call, it renders this particular field as ["Object Object"] even though the data is properly saved in the database.
我建议您自己尝试,因为它在很大程度上取决于版本和驱动程序。
虽然我想问一下您使用的是哪种数据库?
另一种方法是在数组中将您想要的对象定义为模型,然后将该模型用作您的类型:
型号:Class
{
"name": "Class",
"base": "Model",
"strict": true,
"idInjection": false,
"properties": {
"label": {
"type": "string",
"required": true
}
}
}
型号:学生
{
"name": "Student",
"base": "PersistedModel",
"strict": true,
"idInjection": true,
"properties": {
"classes": {
"type": ["Class"],
"required": false
},
}
}