繁体   English   中英

我需要访问一个 POST 请求响应,该响应是一个 SearchSample 对象数组 Angular

[英]i need to access a POST request Response and that Response is An array of SearchSample objects Angular

我的组件中的功能:

searchWithNyckel(){

const formData = new FormData();
formData.append('image', this.updateFormGroup.get('updateProduct.image').value);

this.productService.searchProductNyckel(formData).subscribe(
  data => {
    this.resForSearch= data
    console.log(JSON.stringify(this.resForSearch));
    // this.resForSearch.values()
  }
)
}

我的服务中的功能:

searchProductNyckel(formData: FormData):Observable<SearchRes[]> {
    const url = `https://www.nyckel.com/v0.9/functions/1wx5f2474e1ntc/search`
    return this.httpClient.post<SearchRes[]>(url,formData);
  }

控制台响应:

{
"searchSamples": 
    [{
        "sampleId": "<sampleId>",
        "distance": 0.86,
        "externalId": "<externalId>"
    }]
}

我需要得到 sampleId 和 distance 的值

您可以使用 map 运算符访问响应的内容。

例如:

this.productService.searchProductNyckel(formData).pipe(
   map (response => response.searchSamples)
).subscribe((samples => console.log(sample))

将输出样本数组。

如果您只想访问数组第一项的 1 个变量,可以使用:

this.productService.searchProductNyckel(formData).pipe(
   map (response => response.searchSamples[0].sampleId)
).subscribe((id => console.log(id))

暂无
暂无

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

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