Hello,
i’m call a store action from my view:
this.$store.dispatch('sync_db')
.then(function (response) {
self.$f7.dialog.close();
console.log('finish-last');
})
.catch(function (error) {
console.log(error);
})
and in store.js have this:
sync_db({dispatch}){
var rows_to_upd = [];
return new Promise((resolve, reject) => {
myDB.transaction(function(transaction) {
transaction.executeSql('SELECT * FROM requests WHERE is_sync=0', [], function (tx, results) {
var len = results.rows.length, i;
for (i = 0; i < len; i++){
rows_to_upd.push({ data: results.rows.item(i) });
}
dispatch('sync_mysql', {rows_to_upd});
}, null);
});
})
},
sync_mysql({dispatch}, rows_to_upd){
// return new Promise((resolve, reject) => {
axios({url: 'http://xxx.php', data: rows_to_upd,
auth: {
username: 'xxx',
password: 'xxx'
},
method: 'POST' })
.then(function (response) {
// console.log(response.data);
dispatch('update_sqlite_sync', response.data);
// resolve(response)
})
.catch(
function (error) {
// reject(error)
})
// })
},
update_sqlite_sync({dispatch}, sqlite_to_upd){
const self = this;
var rows = sqlite_to_upd.length, i;
myDB.transaction(function(transaction) {
console.log(rows);
for(i=0; i < rows; i++){
transaction.executeSql('UPDATE requests SET is_sync=1 WHERE uuid="'+sqlite_to_upd[i]+'"', [], function (tx, results) {
console.log('ok update row sqlite');
}, null);
} // fine foreach
return new Promise((resolve, reject) => {
resolve('ok')
})
console.log('finish-update');
resolve('ok');
}); // fine myDB
},
I need to return promise after action “update_sync” when have finished the loop and after i need to refresh page for display new info.
Thanks in advance