Typescript 离子未捕获(承诺):[对象:对象]

Typescript 离子未捕获(承诺):[对象:对象],typescript,ionic3,angular-promise,Typescript,Ionic3,Angular Promise,在Ionic v3中创建数据库时,我尝试填充一些表。 有一个货币表,所有货币都保存在一个JSON文件中。还有一个设置表。我想在创建DB时将所有设置设置为默认值。这是我的代码: import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { SQLite, SQLiteObject } from '@ionic-native/sqlite'; import

在Ionic v3中创建数据库时,我尝试填充一些表。 有一个货币表,所有货币都保存在一个JSON文件中。还有一个设置表。我想在创建DB时将所有设置设置为默认值。这是我的代码:

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { SQLite, SQLiteObject } from '@ionic-native/sqlite';
import { ObjBkpr } from "../../shared/ObjBkpr";

@Injectable()
export class DbApi {
  public db: SQLiteObject;

  static readonly dbName = 'ebookiper_a45fed6.db';
  static readonly dbLocation = 'default';

  currenciesURL = 'assets/files/currencies.json';
  currenciesData:any;

  constructor(public http: HttpClient, private sqlite: SQLite) {
  }

  /** CREATE DATABASE AND TABLES IF NOT EXIST. ALSO INSERT ALL CURRENCIES IN TABLE */
  createDatabase(){
    return this.sqlite.create({ name: DbApi.dbName, location: DbApi.dbLocation }).then((db:SQLiteObject) => {
      this.db = db;
        return this.db.executeSql(DbApi.createTableTrans, []).then(data => {
          return this.db.executeSql(DbApi.createTableCat, []). then(data => {
            return this.db.executeSql(DbApi.createTableCredit, []). then(data => {
              return this.db.executeSql(DbApi.createTableAcc, []). then(data => {
                return this.db.executeSql(DbApi.createTableSetting, []). then(data => {
                  return this.db.executeSql(DbApi.createTableCurr, []). then(data => {
                    this.populateCurrencies();
                    this.populateSettings();
                  });
                });
              });
            });
          });
        });
    });
  }


  populateCurrencies(){
    // Find if there are already records in the table
    let sqlQuery = ' SELECT COUNT(*) AS total FROM ' + DbApi.tableCurrency + ';';
    return this.sqlite.create({ name: DbApi.dbName, location: DbApi.dbLocation }).then((db:SQLiteObject) => {
      return db.executeSql(sqlQuery, []).then(data =>{
        if (data.rows.length > 0) {
          if(data.rows.item(0).total == 0){
            this.getCurrencies();
          }
        }
      });
    });
  }

  getCurrencies(){
    this.http.get(this.currenciesURL)
      .map(res => {
        this.currenciesData = res;
        let total = this.currenciesData.length;
        if( total > 0){
          for(var i=0; i<total; i++){
            let item = this.currenciesData[i];
            let cur = new ObjBkpr();
            cur.designation = item.Code;
            cur.notes = item.Country + ' [' + item.Currency + '] ';
            this.insert(DbApi.tableCurrency, cur);
          }
        }
      }).subscribe(data => {
        // we've got back the raw data
      });
  }

  populateSettings(){
    // Find if there are already records in the table
    let sqlQuery = ' SELECT COUNT(*) AS total FROM ' + DbApi.tableSetting + ';';
    this.sqlite.create({ name: DbApi.dbName, location: DbApi.dbLocation }).then((db:SQLiteObject) => {
      db.executeSql(sqlQuery, []).then(data =>{
        if (data.rows.length > 0) {
          if(data.rows.item(0).total == 0){
            let defaultSettings = [ {key:DbApi.settingPassword,   value:''},
                                    {key:DbApi.settingQuestion1,  value:''},
                                    {key:DbApi.settingQuestion2,  value:''},
                                    {key:DbApi.settingAnswer1,    value:''},
                                    {key:DbApi.settingAnswer2,    value:''},
                                    {key:DbApi.settingPageItems,  value:'50'}, ];
            let nbItems = defaultSettings.length;
            let setting = new ObjBkpr();
            for(var i=0; i<nbItems; i++){
              setting.keyS   = defaultSettings[i].key;
              setting.valueS = defaultSettings[i].value;
              this.insert(DbApi.tableSetting, setting);
            }
          }
        }
      }).catch(e=>{
        console.log('POPULATE SETTINGS: ' + e.message);
      });
    });
  }
}
第一个很好用。但是第二个给了我一个错误,Uncaught(承诺):[object:object] 代码有什么问题?
当使用
.map()
.then()
时,承诺是如何工作的?我刚才看到@kavindhi的一条评论,他显然也有同样的问题

我将我的请求切换到一个新方法,创建了一个要立即执行的
承诺数组。然后就变成这样:

createDatabase(){
  return this.sqlite.create({ name: DbApi.dbName, location: DbApi.dbLocation }).then((db:SQLiteObject) => {
    this.db = db;

    let allQueries:any = [];
    let settingQuery = ' INSERT OR IGNORE INTO ' + DbApi.tableSetting + ' ( ' +
                        DbApi.colKeyS + ',' + DbApi.colValS + ') VALUES (?, ?) ;';

    // Queries to populate table settings
    allQueries.push({query: settingQuery,               arguments:[ DbApi.settingPassword,  '']});
    allQueries.push({query: settingQuery,               arguments:[ DbApi.settingQuestion1, 'Q1']});
    allQueries.push({query: settingQuery,               arguments:[ DbApi.settingQuestion2, 'Q2']});
    allQueries.push({query: settingQuery,               arguments:[ DbApi.settingAnswer1,   '']});
    allQueries.push({query: settingQuery,               arguments:[ DbApi.settingAnswer2,   '']});
    allQueries.push({query: settingQuery,               arguments:[ DbApi.settingPageItems, '50']});
    // Queries to create tables
    allQueries.push({query: DbApi.createTableTrans,     arguments:[]});
    allQueries.push({query: DbApi.createTableCat,       arguments:[]});
    allQueries.push({query: DbApi.createTableCredit,    arguments:[]});
    allQueries.push({query: DbApi.createTableAcc,       arguments:[]});
    allQueries.push({query: DbApi.createTableSetting,   arguments:[]});
    allQueries.push({query: DbApi.createTableCurr,      arguments:[]});
    allQueries.push({query: DbApi.createTableTransCred, arguments:[]});

    return this.executeTransaction(db, allQueries).then(res=>{
      return this.populateCurrencies(); // Populate currencies
    }).catch(err=>{
      console.log('DB PRE-SETTINGS FAILED! ' + JSON.stringify(err));
    });
  });
}
我认为,从我的第一次实现开始,我应该做如下的事情

return this.populateCurrencies().then((res) => {
     this.populateSettings();
})

我希望这有帮助

我刚刚看到@kavindhi的评论,他显然也有同样的问题

我将我的请求切换到一个新方法,创建了一个要立即执行的
承诺数组。然后就变成这样:

createDatabase(){
  return this.sqlite.create({ name: DbApi.dbName, location: DbApi.dbLocation }).then((db:SQLiteObject) => {
    this.db = db;

    let allQueries:any = [];
    let settingQuery = ' INSERT OR IGNORE INTO ' + DbApi.tableSetting + ' ( ' +
                        DbApi.colKeyS + ',' + DbApi.colValS + ') VALUES (?, ?) ;';

    // Queries to populate table settings
    allQueries.push({query: settingQuery,               arguments:[ DbApi.settingPassword,  '']});
    allQueries.push({query: settingQuery,               arguments:[ DbApi.settingQuestion1, 'Q1']});
    allQueries.push({query: settingQuery,               arguments:[ DbApi.settingQuestion2, 'Q2']});
    allQueries.push({query: settingQuery,               arguments:[ DbApi.settingAnswer1,   '']});
    allQueries.push({query: settingQuery,               arguments:[ DbApi.settingAnswer2,   '']});
    allQueries.push({query: settingQuery,               arguments:[ DbApi.settingPageItems, '50']});
    // Queries to create tables
    allQueries.push({query: DbApi.createTableTrans,     arguments:[]});
    allQueries.push({query: DbApi.createTableCat,       arguments:[]});
    allQueries.push({query: DbApi.createTableCredit,    arguments:[]});
    allQueries.push({query: DbApi.createTableAcc,       arguments:[]});
    allQueries.push({query: DbApi.createTableSetting,   arguments:[]});
    allQueries.push({query: DbApi.createTableCurr,      arguments:[]});
    allQueries.push({query: DbApi.createTableTransCred, arguments:[]});

    return this.executeTransaction(db, allQueries).then(res=>{
      return this.populateCurrencies(); // Populate currencies
    }).catch(err=>{
      console.log('DB PRE-SETTINGS FAILED! ' + JSON.stringify(err));
    });
  });
}
我认为,从我的第一次实现开始,我应该做如下的事情

return this.populateCurrencies().then((res) => {
     this.populateSettings();
})

我希望这有帮助

嗨,你解决问题了吗?我也有同样的问题嗨,你解决问题了吗?我也有同样的问题