0

I am experienced java developer and recently working on angular as well, most of the time i use observable<any> as a return type of my http api method or db read method but what is the best way to do this to avoid [object, object] read error or object is undefined.

I would appreciate your help. thanks


 getComments(): Observable<any> {
    return this.http.get(environment.api + '/payments/6');
  }

  addComments(body): Observable<any> {
    console.log(body);
    return this.http.post(environment.api + '/payments/account', body);
  } ```
5
  • When did you encounter this problem? Commented Apr 7, 2021 at 4:52
  • ** when i am reading on html tags, from object of component ** Commented Apr 7, 2021 at 5:04
  • what is wrong with your methods? everything seems well. is it possible for you that explain a little more? Commented Apr 7, 2021 at 5:16
  • 1
    [object, object] read error or object is undefined are due to some bug in your code. Totally unrelated to your question ("what is the best way to do X"). Bugs can creep into the best designs; even to most experienced developers Commented Apr 7, 2021 at 5:22
  • ** actually these are errors are coming as result of i am using different ways to read values from db or api** sometimes i have to use valueChanges to get the object value mainly happening with firebase db. Commented Apr 7, 2021 at 6:51

1 Answer 1

1

in Angular With interpolation, Angular performs the following tasks:

  1. Evaluates all expressions in double curly braces.
  2. Converts the expression results to strings.
  3. Links the results to any adjacent literal strings.
  4. Assigns the composite to an element or directive property

Your problem is with section two, when you pass an object to interpolation, Angular converts object to string and the result will be [object Object], so you have to pass some primitives or use json pipe to interpolate correctly.

Sign up to request clarification or add additional context in comments.

2 Comments

okay but for an observable object of any type how do we do that ?
you can use async pipe {{ (data | async)?.someProp }}

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.