ajjohnson190
ajjohnson190

Reputation: 513

Function inside of class is undefined

I have a class that represents an item in my database.

class Item{

   constructor(serialNumber) {
        this.serialNumber = serialNumber;
    }

    /**
     * Queries database, gets the item and all of it's information
     * @returns query result
     */
    async getItem() {

        //Query database 
        var [rows, fields] = await db.execute('query...', [this.serialNumber]);


        //we have no results, bad entry or is a sales order
        if (rows.length < 1) {
            return rows;
        }

        //checking item
        var check = await _checkIbutton(); //is undefined

        ...

    }

    /**
    * Checks if the item is also an iButton
    * @returns query results if they exist
    */
    async _checkIbutton() {


        var [rows, fields] = await db.execute('Query...', [this.serialNumber]);

        if (rows.length < 1) {
            return { status: false };
        }
        return { status: true, data: rows };
    }

}

The function checkIbutton() checks if the item is a specific type of item. When I call that function in getItem() I get the error: ReferenceError: _checkIbutton is not defined.

_checkIbutton is in the scope of the class and so is getItem. Why is this error is occuring?

Upvotes: 0

Views: 59

Answers (1)

Serhat Levent Yavaş
Serhat Levent Yavaş

Reputation: 141

class Test {

  constructor() {
      this.func1();
  }

  async func1() {

      console.log("asc");
      await this.func2();
  }

  async func2() {
      console.log("22asc");
  }
}
const test = new Test();

you must use this keyword.“this” refers to invoker object (parent object)

Upvotes: 2

Related Questions