Sailsjs一对多空结果
Sailsjs one-to-many empty results
我是 sailsjs 的新手。试图将我的应用程序后端更改为 sailsjs。有一个我需要使用的现有数据库。尝试使用一对多关联时出现此空错误:
{
"Result": [
{
"newCars": [],
"name": 'Someone',
"id": 1
}
]
}
这些是我拥有的两个示例表的结构:
table user
id | name
1 | Someone
table new_car
name | user_id
Audi | 1
BMW | 1
型号:
(我不确定 - association、collection 和 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});
});
我在评论中也添加了一些问题。
您需要将 collection
名称更改为 newcar
。在 Sails
中,每当在 via
、collection
、model
中引用模型时,您都需要使用小写的名称。阅读更多 here。
备注
您需要允许 Sails
创建自己的关联 table。例如,您需要创建模型 User
和 Car
并让 Sails
为您做映射。它是通过在内部创建一个 User_Car
(不一定是相同的名称)table 将 user_id
映射到 car_id
来完成的。这可以简单地通过使用 sails-generate-api
创建两个 apis
来完成
$ sails generate api User
$ sails generate api Car
现在你的模型看起来像:
//User.js
module.exports = {
attributes: {
name: 'string',
cars: {
collection: 'car',
via: 'user'
}
}
};
// Car.js
module.exports = {
attributes: {
name: 'string',
user: {
model: 'user'
}
}
};
现在您可以通过将 HTTP POST
发送到 /user
并将汽车发送到 /car
来创建 User
要创建关联 User=>Car
发送 HTTP POST
至 /user/:id/car/:id
现在,当您通过 GET /user/:id
获取 User
的详细信息时,user[:id]
拥有的所有 Cars
都将被填充。
设法修复它。它可能对像我这样的新手有用。只需要识别主键和外键。
//at the one side
id: {
type: 'integer',
primaryKey: true
}
//at the many side
user: {
columnName: 'user_id',
model: 'user'
}
我是 sailsjs 的新手。试图将我的应用程序后端更改为 sailsjs。有一个我需要使用的现有数据库。尝试使用一对多关联时出现此空错误:
{
"Result": [
{
"newCars": [],
"name": 'Someone',
"id": 1
}
]
}
这些是我拥有的两个示例表的结构:
table user
id | name
1 | Someone
table new_car
name | user_id
Audi | 1
BMW | 1
型号: (我不确定 - association、collection 和 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});
});
我在评论中也添加了一些问题。
您需要将 collection
名称更改为 newcar
。在 Sails
中,每当在 via
、collection
、model
中引用模型时,您都需要使用小写的名称。阅读更多 here。
备注
您需要允许 Sails
创建自己的关联 table。例如,您需要创建模型 User
和 Car
并让 Sails
为您做映射。它是通过在内部创建一个 User_Car
(不一定是相同的名称)table 将 user_id
映射到 car_id
来完成的。这可以简单地通过使用 sails-generate-api
apis
来完成
$ sails generate api User
$ sails generate api Car
现在你的模型看起来像:
//User.js
module.exports = {
attributes: {
name: 'string',
cars: {
collection: 'car',
via: 'user'
}
}
};
// Car.js
module.exports = {
attributes: {
name: 'string',
user: {
model: 'user'
}
}
};
现在您可以通过将 HTTP POST
发送到 /user
并将汽车发送到 /car
User
要创建关联 User=>Car
发送 HTTP POST
至 /user/:id/car/:id
现在,当您通过 GET /user/:id
获取 User
的详细信息时,user[:id]
拥有的所有 Cars
都将被填充。
设法修复它。它可能对像我这样的新手有用。只需要识别主键和外键。
//at the one side
id: {
type: 'integer',
primaryKey: true
}
//at the many side
user: {
columnName: 'user_id',
model: 'user'
}