【发布时间】:2017-06-15 08:57:51
【问题描述】:
我是sailsjs 的新手。试图将我的应用程序后端更改为sailsjs。有一个我需要使用的现有数据库。尝试使用一对多关联时出现此空错误:
{
"Result": [
{
"newCars": [],
"name": 'Someone',
"id": 1
}
]
}
这些是我拥有的两个示例表的结构:
table user
id | name
1 | Someone
table new_car
name | user_id
Audi | 1
BMW | 1
型号: (我不确定 - 关联、收藏和通过的命名)
//UserController.js
module.exports = {
tableName: 'user',
attributes: {
name: {
type: 'string'
},
newCars: { //i can name this anything?
collection: 'newCar', //should this be new_car (table name)?
via: 'user' //this is one-side table name?
}
},
autoCreatedAt: false,
autoUpdatedAt: false
};
//NewCarController.js
module.exports = {
tableName: 'new_car',
attributes: {
name: {
type: 'string'
},
users: {
model: 'User'
}
},
autoCreatedAt: false,
autoUpdatedAt: false
};
控制器:
Role.find(1).populate('newCars').exec(function (err, result){
res.json({Result: result});
});
我也添加了一些我在 cmets 中遇到的问题。
【问题讨论】: