2017-06-21 84 views
0

我收到以下錯誤:無極拋出錯誤

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

我有一個類中聲明,我調用返回一個無極的方法....

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); 
} 

這裏就是我撥打電話

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在executeSql上拋出錯誤,但我不明白爲什麼它沒有打那麼「正確」。

+0

您應該檢查以確保該事件未被多次觸發。 – Ahmad

回答

1

作爲一般規則,不要在構造函數中放置異步事物。你什麼時候準備好都不知道。相反:

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)); 
    } 
} 
+0

我會命名屬性'createdDb'而不是'db'。所以它讀取'this.createdDb.then(db => ...)' – unional

+0

我仍然收到相同的錯誤。 – Adam