简体   繁体   English

与 json-server 的关系

[英]Relationship with json-server

I'm building a fake API with json-server and I came across a problem when passing a query.我正在使用 json-server 构建一个假的 API,我在传递查询时遇到了问题。 I can show the parameters of a relationship when I use _expand , but when it is a relationship within a child it does not work.当我使用_expand时,我可以显示关系的参数,但是当它是子关系中的关系时,它不起作用。

For example, I have this json that I access by passing http://localhost:3000/schedules :例如,我有这个 json,我通过传递http://localhost:3000/schedules来访问它:

{
   id: 1,
   date: "2020-04-25T14:20:00-03:00",
   status: "Novo serviço",
   usersId: 5,
   providersId: 1,
   servicesId: 1,
},

Now to show the relationship with the user I pass the following query:现在为了显示与我通过以下查询的用户的关系:

http://localhost:3000/schedules?_expand=users

It returns as follows:它返回如下:

{
   id: 1,
   date: "2020-04-25T14:20:00-03:00",
   status: "Novo serviço",
   usersId: 5,
   providersId: 1,
   servicesId: 1,
   users: {
      id: 5,
      name: "Carla Pedroso",
      photo: "https://cdn.pixabay.com/photo/2016/01/19/17/48/woman-1149911_960_720.jpg",
      rating: 5,
      addressesId: 1,
   },
},

Well, my question is how I can show the JSON of the addressesId , because I already tried using _expand but without success.好吧,我的问题是如何显示addressesId的 JSON,因为我已经尝试使用_expand但没有成功。

change the schedule property "usersId" to "userId",将计划属性“usersId”更改为“userId”,

should work with应该与

http://localhost:3000/schedules?_expand=user http://localhost:3000/schedules?_expand=user

and the response should be something like that:响应应该是这样的:

{
  id: 1,
  date: "2020-04-25T14:20:00-03:00",
  status: "Novo serviço",
  providersId: 1,
  servicesId: 1,
  userId: 5,
  user: {
    id: 5,
    name: "Carla Pedroso",
    photo: "https://cdn.pixabay.com/photo/2016/01/19/17/48/woman-1149911_960_720.jpg",
    rating: 5,
    addressesId: 1
  }
}

With json-server you can add the parent and children of an element to the result, but it will only bring one level, so it will not bring grandparents or grandchildren's.使用 json-server 您可以将元素的父级和子级添加到结果中,但它只会带来一个级别,因此不会带来祖父母或孙子女的。

In your case instead of looking at the schedules you can look at the users and ask to expand the addresses and embed the schedules在您的情况下,您可以查看用户并要求扩展地址并嵌入时间表,而不是查看时间表

http://localhost:3000/users?_expand=addresses&_embed=schedules

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM