0

Below is my service method:

    public saveCustomFieldsData(customAttributes) {    
        // return this.httpc.post(url, customAttributes, {headers: httpOptions.headers})
        //   .pipe(map(res => res));  //Working
        
        customAttributes.forEach(function (arrayItem) {
          if(arrayItem.action == 'edit') {
            return this.httpc.post(url, customAttributes, {headers: httpOptions.headers})
            .pipe(map(res => res));  //Not working
          }
        });
    }

I am calling this from my component

    this.customFieldsService.saveCustomFieldsData(this.customAttributes).subscribe((res: any[]) => {
        // logic
    });

This is working with commented code which does not have the if condition. But when I try to move this HTTP call inside forEach and if condition, I am getting Property 'subscribe' does not exist on type 'void' error. How do I modify my code to work from inside forEach and if condition?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
vamsi
  • 1,488
  • 3
  • 28
  • 66
  • If you actually typed your function, the compiler could tell you there's a problem. You don't return anything from `saveCustomFieldsData`, returning in the callback to `.forEach` is **not** the same thing (and is ignored for that method anyway). – jonrsharpe Jan 11 '22 at 10:19
  • I am not getting how can I modify my code wrt angular context. – vamsi Jan 11 '22 at 10:27
  • It's unclear what you want to achieve. If you just want to return the first one reached, for example, use a regular `for` loop, so the `return` is actually inside `saveCustomFieldsData`. – jonrsharpe Jan 11 '22 at 10:40

0 Answers0