Adam
Adam

Reputation: 459

Promise throws errors

I am receiving the following error:

{"__zone_symbol__currentTask":{"type":"microTask","state":"notScheduled","source":"Promise.then","zone":"angular","cancelFn":null,"runCount":0}}

I have a class declared that I am calling a method that returns a Promise....

export class TechPRODAO {
sqlite: any;
db: SQLiteObject;

constructor() {
    this.sqlite = new SQLiteMock();

    this.sqlite.create({
        name: 'techpro.db',
        location: 'default'
    }).then((_db: SQLiteObject) => {
        this.db = _db; 
    });
};

public executeSql(sqlstatement: string, parameters: any): Promise<any> {

    return this.db.executeSql(sqlstatement, parameters);
}

Here is where the I make the call

export class AppointmentDAO {
techprodao: TechPRODAO;

constructor(_techprodao: TechPRODAO) {
    this.techprodao = _techprodao;
};

public insertAppointment(appointment: Appointment) {
    console.log("insertAppointment called");
    this.techprodao.executeSql("INSERT INTO appointment (ticketnumber, customername, contactemail, contactphone, status, location, paymenttype, description, hascontract) " +
        "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)", [appointment.ticketnumber, appointment.customername, appointment.contactemail, appointment.contactphone, appointment.status,
            appointment.location, appointment.paymenttype, appointment.description, appointment.hascontract])
        .then((data) => {
            console.log("Inserted into appointment: ticketnumber=" + appointment.ticketnumber);
        }, (error) => {
            console.log("ERROR in insertAppointment: " + JSON.stringify(error));
        });
}

insertAppointment throws the error on the executeSql, but I don't understand why it isn't hitting the "then" properly.

Upvotes: 0

Views: 2188

Answers (1)

user663031
user663031

Reputation:

As a general rule, do not put asynchronous things in the constructor. You'll have no idea when they're ready. Instead:

export class TechPRODAO {
  sqlite: any;
  db: Promise<SQLiteObject>;

  constructor() {
    this.sqlite = new SQLiteMock();

    this.db = this.sqlite.create({
      name: 'techpro.db',
      location: 'default'
    });
  }

  public executeSql(sqlstatement: string, parameters: any): Promise<any> {
    return this.db.then(db => executeSql(sqlstatement, parameters));
  }
}  

Upvotes: 1

Related Questions