我正在尝试使用json-server快速构建后端
我的端点和数据如下:
端点:http://localhost:3000/members/1
产出数据:

现在假设我想做几件事,比如:
接下来是docs和我尝试做的: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的成员创建一个新的报告。在此之后,报表应该存在于成员1的reports数组中。
我的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()
}
})
}
}),发布于 2018-07-05 03:56:22
根据文档和现场演示,您的第一个请求/查询是正确的:http://jsonplaceholder.typicode.com/posts/1?_embed=comments返回post #1加上它的所有注释。
对于你的问题#1,我建议你再次检查你的数据库文件。
对于第二个问题,您可以使用HTTP谓词,如POST和修补程序:https://github.com/typicode/jsonplaceholder#updating-a-resource --它应该更新您的“数据库”文件(json )
发布于 2018-07-05 04:58:21
对于您的问题#1,请使用http://localhost:3000/members/1/reports路由。它将返回成员#1的所有报告
发布于 2021-02-06 12:02:55
db = {}
db.posts = [
{ id: 1, body: 'foo' },
{ id: 2, body: 'bar' },
]
db.comments = [
{ id: 1, post_id: 1 },
{ id: 2, post_id: 1 },
{ id: 3, post_id: 2 },
]
server = jsonServer.create()
router = jsonServer.router(db, { foreignKeySuffix: '_id' })
server.use(jsonServer.defaults())
server.use(router)})
https://stackoverflow.com/questions/51183090
复制相似问题