简体   繁体   English

使用Angular中的Observable使http发布同步

[英]make http post synch using Observable in Angular

I'm trying to implement this but need some help (I'm new to this). 我正在尝试实现此功能,但需要一些帮助(我是新手)。

I currently have, 我目前有

constructor(http: HttpClient, private cvservice: CvService) {
const req = http.post<any>('url', [{
            "name": "ids",
            "license": "GPL version 2"
}])
  .subscribe(
    res => {
    console.log(res);
    var resp = res.Results[0].results.map(x => {
            return {cvid: x.ID, severity: x.CVSS };
    } );
   this.data = [...resp];
    },
    err => {
      console.log("Error occured");
    }
  );
}

This works fine except I need to make the http.post synch (need Results[0] before continuing) and I'm using the suggestion from, How to synchronise Angular2 http get? 除我需要使http.post同步(继续之前需要Results [0])并且我正在使用来自如何同步Angular2 http get的建议外,此方法工作正常 .

I've modified the service as, 我已将服务修改为

export class CvService {
constructor(private http: HttpClient) {
}
url = 'foo';
getIds():Observable<any[]> {
const url='xxx';
return this.http.post(url, [{
            "name": "ids",
            "license": "GNU General Public License GPL version 2"
        }])
        .map(
            res =>  {
                res.Results[0].results.map( x => {
                   return {cvd: x.ID, severity: x.CVSS };
}));
}
}
}

But I think this syntax is wrong. 但是我认为这种语法是错误的。 This gives compile error. 这会产生编译错误。 Can someone please clarify the correct way to use the map function here? 有人可以在这里说明使用地图功能的正确方法吗?

import map from rxjs and use it inside the pipe like this 从rxjs导入地图,并像这样在管道内使用它

return this.http.post(URL, Object)
 .pipe(map(data) => {
    return data['result'].map(res => {
   // Here You can return the entire res, or just the properties you want
   // EX: --> return res OR --> return res.cvd
   });
});

also, make the object outside 另外,把物体放在外面

let cv = [{
  name: "ids",
  license: "GNU General Public License GPL version 2"
}];

You can use it then like this 你可以这样使用它

return this.http.post(URL, cv);

You can implement this way: 您可以通过以下方式实现:

Service 服务

saveUser(): Observable<any> {
    const data = [{"name": "ids", "license": "GNU General Public License GPL version 2"}];

    // For Angular v5 below
    this.http
      .post(url, data)
      .map(res => res.json());       // or .map(res => res), depends what your fetching with

    OR you can pipe it out (Angular 6+)

    this.http
      .post(url, data)
      .pipe(map(({ Results }: any) => Results[0].results.map(item => ({cvid: item.ID, severity: item.CVSS }))));

    // Based from you code when you accessed it this way: res.Results[0].results.map( x => {return {cvd: x.ID, severity: x.CVSS };
}

Component 零件

this.userService
   .saveUser()
   .subscribe(data => console.log(data));

Had created a Stackblitz demo for your reference - This uses different data and mocked POST API for demo purposes only. 已创建了一个Stackblitz 演示供您参考-该演示使用不同的数据和模拟的POST API,仅用于演示目的。

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

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