在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();
第一个工作正常。但是第二个给我一个错误,未捕获(承诺):[object:object]
代码有什么问题?
完全使用.map()
或.then()
时,promise如何工作
答案 0 :(得分:0)
我刚刚看到@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();
})
我希望这会有所帮助!。