4

I am passing an id through a url in my angularfire2 app and collecting it in the onInit method

  ngOnInit() {
    // subscribe to router event
    this.activatedRoute.params.subscribe((params: Params) => {
      this.groupId = params['groupId'];
      // grab specific group
      this.group = this.af.database.object('/groups/'+this.groupId);
      this.group.subscribe(console.log);
    });
  }

the console.log shows perfectly the object I want but I can't then display it the HTML with {{group.data}} instead I get the string [object Object].

I get the same result using a list observable too but in the console log I get a bunch of arrays of the data I want instead of 1 object so an object would be preferable if possible.

What do I have to do to be able to display the data?

1
  • Please post your model demonstrating the data. This issue can't be accurately diagnosed without knowing what that looks like. If data is a list-like object, for example, then the answer is different than if data is a simple key value pair. Commented Mar 4, 2017 at 19:11

2 Answers 2

2

As per the code snippet you provide it look like group is Observable, so you can you async pipe in your template as follows,

<div>
   {{group | async}}
</div>
Sign up to request clarification or add additional context in comments.

Comments

1
console.log(JSON.stringify(yourObject));

or if you want to log what is being omitted down the RXJS stream ..

this.activatedRoute.params.do(x => console.log(x)).subscribe 

if in your HTML you want to display an Object you want to use the JSON pipe ..

instead of ..

{{ yourObject }}

you want ..

{{ yourObject | json }}

If you want to output a list ..

<ul>
    <li *ngFor="let item in list">
        {{ item | json }}
    </li>
</ul>

5 Comments

thanks for the advice but now i am getting: EXCEPTION: Uncaught (in promise): Error: Error in ./GroupsComponent class GroupsComponent - inline template:5:29 caused by: Converting circular structure to JSON when i add the json pipe
the reason you are getting this is explained here .. stackoverflow.com/questions/32334580/… .. i will find a fix for you one sec
the reason this happens is because the | json pipe is, under the hood, simply using JSON.stringify (which converts an object to a string so you can read it) .. if JSON.stringify is passed a JSON object with cyclic references (references that point back to itself) then this error occurs - still looking for a fix
the best i can come up with is .. stackoverflow.com/questions/5410162/… .. which has various solutions - most of which involve using a library (like dojo) to overcome this problem
you could just stringify the property you are interested in or you could just console.log each one and inspect the object in the browser console tab - all the best - if you dont know what properties are on the Object just use Object.keys(yourObject)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.