0

I am trying to get only validationTypeId, validationType. But not working

   this.accessDService.getValidationDetails().subscribe((result) => {
          this.accessDValidationDetails = result;
          if (!result) return;
          this.validationD = result.map(({ validationTypeId, validationType }) => ({ validationTypeId, validationType }));
And here my response body { "id": 32, "validationTypeId": 2, "accessDeviceId": 57, "discountValue": 0, "mins": 15, "validationType": "Discount" }
Allison.A
  • 149
  • 1
  • 2
  • 9
  • **1.** You cannot `return` from the subscription callback. **2.** What exactly would be the error in the implementation? – ruth Sep 02 '21 at 08:42
  • How can I return from subscription callback then? Error is property map doesn't exist on the "result" – Allison.A Sep 02 '21 at 08:47
  • 1
    **Q1.** Subscription callbacks are asynchronous. Variables cannot be returned from inside the subscription. You need to subscribe where the response is required. See [here](https://stackoverflow.com/q/14220321/6513921) for more info on async data. **Q2**. JS [`Array#map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) function could only be invoked upon arrays. Check if the `result` is an array. – ruth Sep 02 '21 at 08:53

1 Answers1

0

If you get just one object from response you can't use map here. You can use map only on arrays.

Just simple:

this.validationID = {validationTypeId: result.validationTypeId, validationType: result.validationType}

Edit:

Then maybe that will help:

import { map } from "rxjs/operators"; 

result.pipe(map(item => this.validationD.push({validationTypeId: item.validationTypeId, validationType: item.validationType})))
abellay
  • 368
  • 2
  • 11