I have posted a similar question earlier, but I was not concise enough. So, here i go!
I am fetching some data, an array of objects, from a website, and then looping thru it to modify it. I am modifying it solely because i need new elements inside each object in an array to later run some "if-statements" on them.
I had found that my loop and if statements work okay without the fetch function. However, when I add it, the if statement that should return true does otherwise.
The example code is at the bottom.
This is the part that works outside the fetch if i run it as is (after i get the data array of objects. i ran the code without fetch, and it was all okay.)
if(time<new Date(new Date(Date.now()).toLocaleString('en-US', {timeZone: 'UTC'})))
{
var time = new Date(new Date(Date.now()).toLocaleString('en-US', {timeZone: 'UTC'}))
//changing time with current time in UTC if time is "smaller"
}
else
{
//do nothing
}
for (let i = 0; i < data.length; i++)
{
data[i]['ontime']=(new Date(new Date(data[i].starttime).toLocaleString('en-US', {timeZone: 'UTC'})))
data[i]['estimatedtime']=dateAdd(new Date(time), 'second', Math.round(data[i].distance.value/50*60*60))
for (let j = 0; j < data.rooms.length; j++)
{/*push and save some elements*/}
if (data[i].ontime > data[i].estimatedtime)
{
//do something
}
else // repeat fetch if the condition is false
{
console.log('unsuitable!')
getData()
}
}
I have tried to make two loops. One for mutating the data array's objects, and the other for if-statements +I had used asynch/await function for these two separate loops, to make sure 2nd loop (if-statements) starts only after the 1st loop finished. it did not seem to work. Previously, i posted that the error was with the date, that it was showing 'invalid date'. I fixed that, but i still have the same problem.
time = new Date('11/2/2019 14:25')
function getData(){
fetch(fetch_url)
.then((response) => {
if (!response.ok)
{
errorcheck.push(response.status)
} // if response is not ok, i store the error code.
else if(response.ok){
response.json()
.then(data => data = data)
.then(() => {
if (data.length>0) // checking if any "data array" has objects
{
if(time<new Date(new Date(Date.now()).toLocaleString('en-US', {timeZone: 'UTC'})))
{
var time = new Date(new Date(Date.now()).toLocaleString('en-US', {timeZone: 'UTC'}))
//changing time with current time in UTC if time is "smaller"
}
else
{
//do nothing
}
for (let i = 0; i < data.length; i++)
{
data[i]['ontime']=(new Date(new Date(data[i].starttime).toLocaleString('en-US', {timeZone: 'UTC'})))
data[i]['estimatedtime']=dateAdd(new Date(time), 'second', Math.round(data[i].distance.value/50*60*60))
for (let j = 0; j < data.rooms.length; j++)
{/*push and save some elements*/}
if (data[i].ontime > data[i].estimatedtime)
{
//do something
}
else // repeat fetch if the condition is false
{
console.log('unsuitable!')
getData()
}
}
}
else
{
console.log('no object in data!')
getData() //if data array had no objects inside, i repeat the fetch
}
}
)
}
}
)
}
Aucun commentaire:
Enregistrer un commentaire