async pipe gets [object Object] in Angular 6

Robert Tudor

I am trying to implement "ag-Grid" into my project and I'm trying to populate it by fetching data from a local JSON mock file.

Considering it's my first time using "ag-Grid" and I'm at the very begining with my Angular 6 experience this is what I've come with so far.

This is the structure of my JSON Object

{
  "response": {
    "code": 200,
    "body": {
      "email": "[email protected]",
      "displayName": "User 1",
      "actions": [
        {
            "note": "some note",
            "author": {
              "displayName":"James Potter"
            },
            "createdAt": "1520262442000",
            "updateAt": "20180203073000"
        },
        {
            "note": "Some note",
            "author": {
              "displayName":"Bilbo Bagins"
            },
            "createdAt": "1496672283000",
            "updateAt": "20180203073000"
        }
      ]
    }
  }
}

This is my component

export class MockupViewComponent implements OnInit {

    columnDefs = [
        {headerName: 'Name',        field: 'response.body.actions.author.displayName'},
        {headerName: 'Status',      field: 'response.body.actions.note'},
        {headerName: 'Log Date',    field: 'response.body.actions.createdAt'},
        {headerName: 'Update Date', field: 'response.body.actions.updateAt'}
    ];

    rowData: any;

    constructor( public mockGetService: MockupParseService ) {
    }

    ngOnInit() {
        this.mockGetService.getJSON().subscribe(data => {
            this.rowData = data;
        });
    }
}

This is my JSON get service

export class MockupParseService {  // ReadMockService
  constructor(private http: HttpClient) {
  }

  getJSON(): Observable<any> {
    return this.http.get('./assets/mockup/history.json');
  }
}

And finally, this is my template

<ag-grid-angular
        style="width: 1000px; height: 500px;"
        enableSorting="true"
        enableFilter="true"
        class="ag-theme-balham"
        [rowData]="rowData | async"
        [columnDefs]="columnDefs"
></ag-grid-angular>

What I'm having trouble with is rowData. Just treat it as a normal {{rowData | async}} interpolation for the sake of it.

I understand that async sort of expects an array and it's getting an Object, hence the error. I tried removing async with no positive result. I've tried to get the JSON file straight from the component and a number of other solutions with no relative success. I'm pretty sure it's either a matter of concepts I'm missing or it's just a distraction mistake.

Phix

The async pipe doesn't expect a data type, it's used for dealing with (usually) observables of async data, such as an HTTP call. If you subscribe to the observable, you'll need to unsub on the components destroy lifecycle hook to prevent memory leaks which the async pipe takes care of for you.

You can use the map operator to return the actions property in the initial http call:

getJSON(): Observable<any> {
   return this.http.get('./assets/mockup/history.json').pipe(map(res => res.actions))
}

Or subscribe to the data as you are, but pass in the right prop:

ngOnInit() {
    this.mockGetService.getJSON().subscribe(data => {
        this.rowData = data.actions;
    });
}

When you subscribe to the observable in the component, don't use the async pipe in the template. Only use it for passing the observable to the input directly.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Angular async pipe and object property

Angular Async Pipe - Array in Object in Observable

angular - using async pipe on observable<Object> and bind it to local variable in html

Using an array from Observable Object with ngFor and Async Pipe Angular 2

ERROR: InvalidPipeArgument: '[object Object]' ngrx async pipe

angular 6 filter the async pipe results

Angular 6: OnChanges does not work with async pipe

Angular 4: InvalidPipeArgument: '[object Object]' for pipe 'AsyncPipe'

How to pass async data to child components in an object (Angular6)

Async pipe does not fill object data into template

How to iterate over an array in an object with async pipe

Using async pipe looses reference to object type

why there is error in async pipe "InvalidPipeArgument: '[object Object],[object Object],' for pipe 'AsyncPipe'"?

parse [object Object] Angular 6

Cannot find a differ supporting object ,Async pipe and ngFor problems in angular2

Angular/angularfire2 - Read a document observable and save the data to an object. No async pipe

Why Is It That My Object Gets Added 6 Times?

angular async validator object not found

Filter object array with pipe Angular 2

Read object field from observable using async pipe

Sort Array of object by object field in Angular 6

Angular async pipe with mergeMap

Angular Async Pipe on a getter

How to get data collections within the JSON Object using async in Angular 6?

Get object by ID angular 6:

Angular 6 find and delete object

'[object Object],[object Object]' for pipe 'AsyncPipe'

JavaScript().Serializer() gets [object, object]

InvalidPipeArgument: '[object Object]' for pipe 'AsyncPipe'