This.get(...) is null Stopping code execution

this.get(…) is null is stopping my code from executing and is showing as an error in the console . Is this expected behavior? In my case that value can be null sometimes and I have an if statement to tell state what should happen.

@yefan15 Could you share more code and context? :pray:

This kind of error typically happens due to Javascript’s often-confusing rules about this. When you’re really inside a method on an Ember.Object, this.get would never be null, so most likely your this is pointing at something else. For example:

export default Component.extend({
  doSomething() {
    // this would work
    this.get('myName');
    setTimeout(function() {
       // this would not, because the function 
      // we're inside has a different `this`.
       this.get('myName');
    });
  }
});

The easiest solution in the case of a function like above is to switch to => instead, which preserves this.

(@yefan15 please don’t flag your own posts. Even if you figured out the solution, other people are likely to have similar questions, so I’d prefer not to delete them.)