简体   繁体   中英

Show property form json object with angular http

I have an object like this:

export interface CameraNodeValue { 
    readonly id?: number;
    readonly dt?: string;
    readonly countUp?: number;
    readonly countDown?: number;
}

and I try to get from endpoint the value from the property countUp

So this is how I subscribe in component on service:

 cameraNodevalue: Array<CameraNodeValue> = [];

 showLineChart() {
    this.sensorNodeService
      .cameraDataInInterval(2, '2021-01-18 00:00:00', '2021-01-30 23:59:00', '1')
      .pipe(
        map((data=> data['countUp'].map(obj => {

          return {
            countUp: obj.countUp
          }
        }))))
      .subscribe((data: any) => {
        console.log('chart', data);
      });
  }

But I get this error:

core.js:4442 ERROR TypeError: Cannot read property 'map' of undefined

So what I have to change?

Thnak you

ahh, oke Yes, I get this back:

0:
count_down: 2
count_up: 2
dt: "2021-01-18T08:28:00Z"
id: 2

So but when I do this:

showLineChart() {
    this.sensorNodeService
      .cameraDataInInterval(2, '2021-01-18 00:00:00', '2021-01-30 23:59:00', '1')
      .pipe(
        map((data=> data['count_up'].map(obj => {

          return {
            countUp: obj.countUp
          }
        }))))


      .subscribe((data: any) => {
        console.log('chart', data);
      });

I still get this error:

ERROR TypeError: Cannot read property 'map' of undefined

data is an array. Use data.map((obj: any) => obj.count_up)) to get an array with the count_up values from every object in data .

showLineChart() {
  this.sensorNodeService
    .cameraDataInInterval(2, '2021-01-18 00:00:00', '2021-01-30 23:59:00', '1')
    .pipe(
      map(data => data.map((obj: any) => obj.count_up))
    )
    .subscribe((data: any) => {
      console.log('chart', data);
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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