我是sailsjs的新手。嘗試將我的應用程序後端更改爲sailsjs。有一個我需要使用的現有數據庫。獲取此空的錯誤嘗試使用一個-to-many關聯:Sailsjs一對多空結果
{
"Result": [
{
"newCars": [],
"name": 'Someone',
"id": 1
}
]
}
這些是我有兩個示例表的結構:
table user
id | name
1 | Someone
table new_car
name | user_id
Audi | 1
BMW | 1
型號: (我不知道namings的 - 協會,收集和VIA)
//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});
});
我在評論中添加了一些問題。
謝謝,但仍然沒有運氣後進行更改你建議。看我的表格結構和你的評論,應該'通過'是'用戶'或'user_id'。注意,嘗試將其更改爲user_id,但仍爲空結果:( – aiiwa
是由'Sails'適配器創建的'user_id'字段? –
不,我的數據庫在嘗試sailsjs之前存在。user(id,name)和new_car(name,user_id) – aiiwa