15

What's the best way to prevent errors in console from objects that are still undefined?

Let's say I have this

name : string;
constructor(private data: DataService) {
this.data.name.subscribe(res => this.name = res);
}

Inside my html I have this

<p> {{name}}</p>

When I load the page I get _co.name is not defined, but the page still shows the value of name. The component is loading before getting the data I suppose.

What's the best approach to prevent this?

I saw ngIf is not null or something like that, is an option. But then I saw something about Resolve.

2
  • It would be better to paste actual call context.... so template mb? Commented May 22, 2018 at 17:29
  • Use an object instead set to null and check if set in the html {{ info?.name }} EDIT : I don't get what is _co but you can also try _co?.name Commented May 22, 2018 at 17:44

4 Answers 4

27

Multiple ways: You can use any one suitable to you.

1. Adding ngIf : If name is undefined or null or '' it will not render the element and prevent errors in console. When name gets defined value it will automatically update the view.

*ngIf="name"

2. Adding async pipe : View will update whenever name gets defined. It waits for name to get defined (or resolved). (name should be a promise or an observable for this to work.)

{{ name | async }}

3. Adding fallback value : This is simply or condition. If name is undefined or null or '' , you can decide which fallback value to assign . {{ name || "" }}

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

4 Comments

Thank you, this is what I was looking for
@kachus22 Great, can you accept the answer? You can click on the gray tick mark to make it green. So it becomes accepted answer.
how to do it with [(ngModel)] = "a.b.c" where we cant use ? operator
Point 2 is wrong. name is a string, not a promise or an observable. Hence you cannot you async pipe on it. angular.io/api/common/AsyncPipe
6

Just initialize your variable

name : string = "";

or you can do it inside of the constructor

this.name = "";

Comments

2

As mentioned in previous responses you can use {{ name | async }} but remember if you want to use {{ name | async }}, name must be a promise or an observable.
Otherwise you'll get an error like this :

ERROR Error: InvalidPipeArgument: 'xxx' for pipe 'AsyncPipe'

Comments

1

You can use {{name?.value}} or {{name?}} inside component.html. Can make treatment to like this in compoment.ts ↓

   ````

    this.route.paramMap.pipe(
      map((param: ParamMap) => {
        // @ts-ignore
        return param.params.id;
      })
    ).subscribe(prodId => {
      this.id = prodId;
      this.productService.getSingleProduct(this.id).subscribe(prod => {
        this.product = prod;
        if (prod.images !== null) {
          this.thumbimages = prod.images.split(';');
        }`enter code here`
      });
    });


  ````

Comments

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.