Search code examples
ionic-frameworkionic3angularfire2angularfire5

Ionic 3 angularfire2 print out [object Object] data


I have a task management apps where i wan't user to be able to see their task in each of the task sections just like in the photo: Click here to see the photo

I can get the data from the database and print it out in JSON format as you can see in the photo where i labeled it as "task get from database", but the problems is I cannot display it like the "Demo only" where you can see in the photo.

I'm using angularfire2 to get the data from firebase realtime-database.

Here are the console for the data: The console of the data

Here are the code from provider:

getTaskSectionAndTaskList(projBoardKey) {
    this.taskSectNTaskListRef = this.afDatabase.list('taskSection/' + projBoardKey);

    return this.taskSectNTaskListRef;
}

Here are the code from .ts file:

...
taskList$: Observable<Task[]>;

constructor(..., private tasksecProvider: TasksectionProvider) {
    ...

    this.taskList$ = this.tasksecProvider.getTaskSectionAndTaskList(this.selectedBoardKey)
    .snapshotChanges()
    .map(changes => {
        return changes.map(c => ({
            key: c.payload.key,
            ...c.payload.val()
        }));
    });
}

Here are the code for HTML file:

<div *ngFor="let taskSectionList of taskList$ | async">
    <ion-slide>
        <ion-card>
            <ion-card-header>
                <p class="task-section-title">{{ taskSectionList.taskSectionTitle }}</p>
            </ion-card-header>
            <ion-card-content>
                <div>
                    <div class="for-demo-only">
                        <div>
                            <p>Task Name</p>
                        </div>
                    </div>
                    <div>
                        {{ taskSectionList.task | json }}
                    </div>
                </div>
            </ion-card-content>
        <ion-card>
    </ion-slide>
</div>

Solution

  • I have solved my problems by using pipes:

    Here are the code for the pipes:

    import { Pipe, PipeTransform } from '@angular/core';
    
    /**
     * Generated class for the GettaskPipe pipe.
     *
     * See https://angular.io/api/core/Pipe for more info on Angular Pipes.
     */
    @Pipe({
       name: 'gettask',
    })
    export class GettaskPipe implements PipeTransform {
      /**
       * Takes a value and makes it lowercase.
       */
      transform(obj: any) {
    
        let tempResult = [];
        let result = [];
    
        for (var key in obj.task) {
          if (obj.task.hasOwnProperty(key)) {
            result.push({
                key: key,
                ...obj.task[key]
            });
          }
        }
    
        return result;
      }
    }
    

    Here is what i made a changes to the HTML file:

    <div *ngFor="let taskSectionList of taskList$ | async">
    <ion-slide>
        <ion-card>
            <ion-card-header>
                <p class="task-section-title">{{ taskSectionList.taskSectionTitle }}</p>
            </ion-card-header>
            <ion-card-content>
                <div>
                    <div class="for-demo-only">
                        <div>
                            <p>Task Name</p>
                        </div>
                    </div>
                    <div *ngFor="let task of taskSectionList | gettask">
                        {{ task.taskName }}
                    </div>
                </div>
            </ion-card-content>
        <ion-card>
    </ion-slide>
    

    Now i can get the task name to be displayed like the demo, just need to apply some css to it then it will look like that already.