如何使用 JavaScript Traverson HAL $all 元选择器?

问题描述 投票:0回答:1

根据 JavaScript Traverson HAL 文档

对于嵌入式数组,您还可以使用元选择器 $all, 它对嵌入文档进行操作:如果将 ht:post[$all] 传递给 按照方法,您会收到完整的帖子数组,而不是 个人帖子资源。包含 $all 的链接关系只能是 作为最后一个元素传递,它仅适用于嵌入式 文件。此外,它只能与 get 和 getResource 一起使用, 不适用于 post、put、delete、patch 或 getUri。

但是,当我打电话时:

    traverson
        .from("http://localhost:8080/api")
        .follow("cars[$all]")
        .getResource(
            function(error, document) {
                console.log(document);
        });

浏览器控制台为

document
记录一个空数组。
http://localhost:8080/api/cars
处的 HAL 响应为:

{
  "_embedded" : {
    "cars" : [ {
      "id" : 1,
      "name": "hotrod",
      "_links" : {
          "self" : {
             "href" : "http://localhost:8080/api/cars/1"
          }
      }
    },{
      "id" : 2,
      "name": "junker",
      "_links" : {
          "self" : {
             "href" : "http://localhost:8080/api/cars/2"
          }
      }
   }]
   },
    "_links" : {
    "self" : {
      "href" : "http://localhost:8080/api/cars"
    },
    "profile" : {
      "href" : "http://localhost:8080/api/profile/cars"
    }
  }
} 

为什么我调用

.follow("cars[$all]")
会导致空数组而不是汽车对象数组?

javascript spring-data-rest hateoas hal hal-json
1个回答
0
投票

我需要点击汽车链接,然后引用汽车数组:

    traverson
    .from("http://localhost:8080/api")
    .follow("cars")
    .follow("cars[$all]")
    .getResource(
        function(error, document) {
            console.log(document);
    });

我认为

cars[$all]
正在跟踪
cars
资源,并且
[$all]
检索了嵌入的汽车数组。

© www.soinside.com 2019 - 2024. All rights reserved.