Обратный вызов вызван перед процессом process

121
12

Эй, я использую базы данных телефонных карт и пытаясь понять, почему мой callback вызывается до завершения процесса процесса, ниже приведен код js и журнал, который указывает мою заявку

Вот код: (Метод, который вызывается первым, является insertIntoDB (x, y);)

    var databaseVersion = 1;

function createTable(tx, tableName, tableColumns){
var sqlStatement = 'CREATE TABLE IF NOT EXISTS '+tableName+' (_id unique, ' + tableColumns+')';
tx.executeSql('DROP TABLE IF EXISTS '+tableName);
tx.executeSql(sqlStatement);
console.log('now doInsertion (if successful should be called');
}

function createMenuDataBase(tx){
var tableName ="element";
var tableColumns = ["name", "resource","remoteData", "picture", "pageId", "type",
"localAction", "replaceText", "textSize", "textColor","background", "sortOrder"];
createTable(tx, tableName, tableColumns);
}

function insertIntoDB(tableName,json){
checkDatabaseVersion(doInsertion,tableName,json);
}

function doInsertion(dbName,json){
console.log('starting doInsertion');
var sqlStart = "INSERT INTO "+dbName+" (";
var sqlEnd =");"
$.each(json.list, function(i, object) {
var first = true;
var columns="";
var values = ") VALUES ("

for (property in object) {
// alert(property);
if(property!=undefined){

if(first){
columns +=property;
values +=object[property];
first=false;
} else {
columns += ', '+property;
values +=', '+object[property];
}
}
}
console.log(sqlStart+columns+values+sqlEnd);
});
}

function checkDatabaseVersion(callback,dbName,json){
var localDatabaseVersion = localStorage.getItem("localDatabaseVersion");
alert(localDatabaseVersion);
if(localDatabaseVersion == 'undefined' || localDatabaseVersion == null){
var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
db.transaction(createMenuDataBase, errorCB, callback(dbName,json));

//localStorage.setItem("localDatabaseVersion",databaseVersion);
}
}

И вот ловушка от кордовы/моего приложения:

08-01 12:09:02.857: D/DroidGap(24433): onMessage(spinner,stop)
08-01 12:09:03.467: D/CordovaLog(24433): starting doInsertion
08-01 12:09:03.467: D/CordovaLog(24433): file:///android_asset/www/js/Database.js: Line 24 : starting doInsertion
08-01 12:09:03.472: D/CordovaLog(24433): INSERT INTO element (name, resource, id, type, appId, sortOrder, background, picture, textColor, remoteData, pageId, localAction, replaceText, active, textSize) VALUES (menu0.9747386311565038, null, 9765, img, 1, 0, null, /content/picture/menu/product, null, null, mainMenu.menu, showProductCatalog(product), null, Y, 0);
08-01 12:09:03.477: D/CordovaLog(24433): file:///android_asset/www/js/Database.js: Line 46 : INSERT INTO element (name, resource, id, type, appId, sortOrder, background, picture, textColor, remoteData, pageId, localAction, replaceText, active, textSize) VALUES (menu0.9747386311565038, null, 9765, img, 1, 0, null, /content/picture/menu/product, null, null, mainMenu.menu, showProductCatalog(product), null, Y, 0);
08-01 12:09:03.477: D/CordovaLog(24433): INSERT INTO element (name, resource, id, type, appId, sortOrder, background, picture, textColor, remoteData, pageId, localAction, replaceText, active, textSize) VALUES (menu0.9847155498013517, null, 9753, img, 1, 1, null, /content/picture/menu/social, null, null, mainMenu.menu, showSocialMedia(socialmedia), null, Y, 0);
08-01 12:09:03.477: D/CordovaLog(24433): file:///android_asset/www/js/Database.js: Line 46 : INSERT INTO element (name, resource, id, type, appId, sortOrder, background, picture, textColor, remoteData, pageId, localAction, replaceText, active, textSize) VALUES (menu0.9847155498013517, null, 9753, img, 1, 1, null, /content/picture/menu/social, null, null, mainMenu.menu, showSocialMedia(socialmedia), null, Y, 0);
08-01 12:09:03.482: D/CordovaLog(24433): INSERT INTO element (name, resource, id, type, appId, sortOrder, background, picture, textColor, remoteData, pageId, localAction, replaceText, active, textSize) VALUES (menu0.5451258043474583, null, 9759, img, 1, 2, null, /content/picture/menu/store, null, null, mainMenu.menu, menu(store), null, Y, 0);
08-01 12:09:03.482: D/CordovaLog(24433): file:///android_asset/www/js/Database.js: Line 46 : INSERT INTO element (name, resource, id, type, appId, sortOrder, background, picture, textColor, remoteData, pageId, localAction, replaceText, active, textSize) VALUES (menu0.5451258043474583, null, 9759, img, 1, 2, null, /content/picture/menu/store, null, null, mainMenu.menu, menu(store), null, Y, 0);
08-01 12:09:03.482: D/CordovaLog(24433): INSERT INTO element (name, resource, id, type, appId, sortOrder, background, picture, textColor, remoteData, pageId, localAction, replaceText, active, textSize) VALUES (menu0.6883956226802171, null, 9762, img, 1, 3, null, /content/picture/menu/showroom, null, null, mainMenu.menu, showCollection(collection), null, Y, 0);
08-01 12:09:03.482: D/CordovaLog(24433): file:///android_asset/www/js/Database.js: Line 46 : INSERT INTO element (name, resource, id, type, appId, sortOrder, background, picture, textColor, remoteData, pageId, localAction, replaceText, active, textSize) VALUES (menu0.6883956226802171, null, 9762, img, 1, 3, null, /content/picture/menu/showroom, null, null, mainMenu.menu, showCollection(collection), null, Y, 0);
08-01 12:09:03.487: D/CordovaLog(24433): INSERT INTO element (name, resource, id, type, appId, sortOrder, background, picture, textColor, remoteData, pageId, localAction, replaceText, active, textSize) VALUES (menu0.21612715546682493, null, 9768, img, 1, 4, null, /content/picture/menu/beskeder, null, null, mainMenu.menu, showNotificationList(notificationList), null, Y, 0);
08-01 12:09:03.487: D/CordovaLog(24433): file:///android_asset/www/js/Database.js: Line 46 : INSERT INTO element (name, resource, id, type, appId, sortOrder, background, picture, textColor, remoteData, pageId, localAction, replaceText, active, textSize) VALUES (menu0.21612715546682493, null, 9768, img, 1, 4, null, /content/picture/menu/beskeder, null, null, mainMenu.menu, showNotificationList(notificationList), null, Y, 0);
08-01 12:09:03.497: D/CordovaLog(24433): now doInsertion (if successful should be called
08-01 12:09:03.497: D/CordovaLog(24433): file:///android_asset/www/js/Database.js: Line 7 : now doInsertion (if successful should be called

спросил(а) 2021-01-19T21:52:08+03:00 9 месяцев, 1 неделя назад
1
Решение
66

Вы вызываете обратный вызов вместо его передачи:

db.transaction(createMenuDataBase, errorCB, callback(dbName,json));

callback(dbName,json) //Calls the function and passes the result to 'db.transaction' as callback but it not a function since the result is not a function.

Попробуй это:

db.transaction(createMenuDataBase, errorCB, callback);

Или в случае, если callback нуждается в этих аргументах, тогда:

db.transaction(createMenuDataBase, errorCB, callback.bind( null, dbName, json ));

ответил(а) 2021-01-19T21:52:08+03:00 9 месяцев, 1 неделя назад
Ваш ответ
Введите минимум 50 символов
Чтобы , пожалуйста,
Выберите тему жалобы:

Другая проблема