使用Promise的查询数据库返回null

时间:2018-09-26 15:33:53

标签: javascript ionic3

我正在尝试使用sqlite数据库从离子数据库中选择所有数据。因此,据我所知dbexecutesql是一个异步函数,如果我错了,请纠正我我试图先查询数据并将其存储到对象数组中,然后将该对象数组用于下一个函数。在执行诺言之后,结果为空,我试图找出我错了哪一部分。这是我第一次在javascript中使用promises

下面是我的代码

  async getTotal(){
    for(let records of this.records){   
       await this.selectPresentWeekData(records.weekid,records.croppingid); // asynchronous functions contains query in database
       this.loopThroughValues(); // must get the value from the query
       this.loopObjectAccess(); // calculates the total using the values from the query
       this.pushDataToShow(records.weekid); // push the query data to a new object
     }
}

selectPresentWeekData(weekid,cropid){
  console.log('Collecting input data'+weekid+' cropid '+cropid);
      this.sqlite.create({name: 'ionicdb.db',location: 'default'}).then((db: SQLiteObject) => {
        const asyncQuery = [
          'SELECT * FROM input_expenses WHERE input_expenses.week_input_rowid = "'+weekid+'" AND input_expenses.input_crop_rowid = "'+cropid+'"',
          'SELECT * FROM labor_expenses WHERE labor_expenses.week_labor_rowid = "'+weekid+'" AND labor_expenses.labor_crop_rowid = "'+cropid+'"',
          'SELECT * FROM food_expenses WHERE food_expenses.week_food_rowid = "'+weekid+'" AND food_expenses.food_crop_rowid = "'+cropid+'"'
        ];
        Promise.all(asyncQuery.map(asyncQuery =>{
          db.executeSql(asyncQuery,{})
        })).then(res => {
            console.log(res);
        }).catch(() => console.log('error'));
      }).catch(e => console.log(e.message));
}

最重要的功能是sql查询,我尝试了promise函数,结果为空,我不知道为什么。该查询是正确的,我只是遵循了一些有关在异步函数上使用Promise的教程。

我希望有人帮我解决这个问题

0 个答案:

没有答案