Milan_w Milan_w -4 years ago 92
TypeScript Question

Cannot read property 'lockDatabase' of undefined (this)

I'm getting this error because 'this' is out of scope I guess, but I can't find a way to fix it. I have tried using the fat arrow and then the function does run. But then there is the problem that I don't have a callback value that I have to use? Here is my code:

this.checkIfDatabaseIsLocked(function(res) {
//If true database is locked
console.log("result checkIfDatabaseIsLocked: " + res);
if (res) {
return;
}
//LockDatabase
this.lockDatabase().then(
result => {


Any ideas? Thanks in advance!!!

Answer Source

Use an arrow function here to ensure this remains the value you expect:

this.checkIfDatabaseIsLocked(res => {
      //If true database is locked
      console.log("result checkIfDatabaseIsLocked: " + res);
      if (res) {
        return;
      }
      //LockDatabase
      this.lockDatabase().then(
        result => {
... and so on
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download