Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
Hi All
please can someone help me with the data return
I can't get data from all rows for extension
I'm using the code below
for (var d = 0; d <= 600; d++) {
var requestPage = [{
qTop : d * 100,
qLeft : 0,
qWidth : 10,
qHeight : 100
}];
self.backendApi.getData(requestPage).then(function(dataPages) {
for (var e = 0; e <= dataPages[0].qMatrix.length; d++) {
dados.push(dataPages[0].qMatrix[e][0].qText);
}
});
}
console.log(dados);
I don't understand why the data doesn't appear when I use the console.log
Thanks a lot for the help
I ended up using the code below
became functional
async function obterDados(){
for (var d = 0; d < 3; d++) {
var requestPage = [{
qTop : 100 * d,
qLeft : 0,
qWidth : 1,
qHeight : 1000
}];
const dadosObtidos = await self.backendApi.getData(requestPage).then(function(dataPages) {
return dataPages[0].qMatrix;
})
for (var e = 0; e < dadosObtidos.length; e++) {
//console.log(dadosObtidos);
var texto = "<tr><td>" + dadosObtidos[e][0].qText + "</td></tr>";
document.querySelector('#tabelanova').insertAdjacentHTML('beforeend',texto);
}
}
}
obterDados();
Thanks to all for your help.
It seem because the getData is asynchrouns fucntion
so the for loop is finishing to run, before all the requests (promises) are finished (resolved)
so when u console.log the the data is not yet inside "dados"
try to console.log in the end of the
then(function{
....
console.log(dados);
}
to check it.
you will get few logs, but the last one should be full.
doing so it returns the first and last loop.
the rest gave as a result undefined
Is it possible to make synchronous or make some kind of execution queue?
seem u have error in your second for loop:
for (var e = 0; e <= dataPages[0].qMatrix.length; d++) {
dados.push(dataPages[0].qMatrix[e][0].qText);
}
the loop is on var e, and u do d++, u should do e++
u can push all the promises into an array
and then use promise.all (google it if u r not familiar with it)
and when it resolves, it means all the promises resolved.
I ended up using the code below
became functional
async function obterDados(){
for (var d = 0; d < 3; d++) {
var requestPage = [{
qTop : 100 * d,
qLeft : 0,
qWidth : 1,
qHeight : 1000
}];
const dadosObtidos = await self.backendApi.getData(requestPage).then(function(dataPages) {
return dataPages[0].qMatrix;
})
for (var e = 0; e < dadosObtidos.length; e++) {
//console.log(dadosObtidos);
var texto = "<tr><td>" + dadosObtidos[e][0].qText + "</td></tr>";
document.querySelector('#tabelanova').insertAdjacentHTML('beforeend',texto);
}
}
}
obterDados();
Thanks to all for your help.