我正在尝试使用json-server构建快速后端
我的端点和数据如下:
端点:http://localhost:3000/members/1
输出数据:
现在假设我想做几件事,比如:
- 获取成员的所有报告。
按照文档,我试图做:
http://localhost:3000/members/1?_embed=reports
获取reports
数组。但这是对我空数组的回应:
{
id: 1,
username: "member1",
firstName: "Miss Osbaldo",
lastName: "Wisozk",
password: "123456",
role: "member",
reports: [ ]
}
- 我想将一些
report
对象添加到reports
数组中。我怎样才能做到这一点?
例如id=1
创建新报告的成员。之后该报告应该存在于reports
成员 1 的数组中。
我的generates.js
文件:
return {
members: _.times(30, function (n) {
return {
id: (n + 1),
username: 'member' + (n + 1),
firstName: faker.name.prefix() + ' ' + faker.name.firstName(),
lastName: faker.name.lastName(),
avatar: faker.image.avatar(),
address: faker.address.streetAddress("###") + ' ' + faker.address.city() + ' ' + faker.address.county(),
phone: faker.phone.phoneNumberFormat(),
division: faker.random.arrayElement(division),
password: "123456",
role: "member",
reports: _.times(5, function (n) {
return {
id: n,
date: faker.date.weekday(),
achievement: faker.lorem.sentence(),
issues: faker.random.arrayElement(issues),
descriptions: faker.lorem.paragraphs(),
comment: faker.lorem.sentences()
}
})
}
}),