typescript - 离子未捕获(承诺):[对象:对象]
问题描述
在 Ionic v3 中创建数据库时,我试图填充一些表。有一个货币表,所有货币都保存在一个 JSON 文件中。还有一个设置表。我想在创建数据库时将所有设置设置为默认值。所以这是我的代码:
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);
});
});
}
}
注意线条
this.populateCurrencies();
this.populateSettings();
第一个工作正常。但是第二个给了我一个错误,Uncaught(in promise): [object: object]
代码有什么问题?.map()
使用 the或.then()
exact时 Promise 是如何工作的
解决方案
我刚刚看到@kavindhi 的评论,他显然有同样的问题。
我将我的实现切换到一个新方法,创建一个Promises
要立即执行的数组。然后变成了这样:
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();
})
我希望这有帮助!。
推荐阅读
- laravel - 在路由中同时调用中间件和控制器方法
- java - Java Spring 一个类的多个节点
- dialogflow-es - 富媒体的意图行动
- r - R闪亮的映射输出到输入
- azure - TeamCity Azure 资源管理器模板:缺少 MSBuild
- python-2.7 - 将生成器对象转换为列表会导致值错误
- action - 无法使用两个以上的动作包来创建多个自定义动作
- swift - UIcollectionView 单元格设置 ZIndex 。我想使用 UICollectionView 创建卡片单元
- amazon-s3 - 如何将包含 JSON 字段的 CSV 文件加载到 Amazon Athena
- c# - UNITY,SHADER 战争迷雾着色器导致统一崩溃