I have read some posts that are related with my question but I'm a bit lost. What I'm trying to do is to add values of a collection in a Array and value of Docker in an other one. I can put the values in the 2 arrays but suddently the seconde one lose all his data.
There is my code:
triggerState = Meteor.bindEnvironment(function() {
console.log("Start triggerState");
//clean the 2 arrays
stateLastCheck.length = 0;
stateDockerLastCheck.length = 0;
//add the collection value in the array
InfosContainers.find().forEach(Meteor.bindEnvironment(function(ctn) {
stateLastCheck.push(ctn.stateContainer);
console.log("stateLastCheck " + stateLastCheck.length);
}));
console.log("end stateLastchek");
//take the actual values
docker.listContainers({
all: true
}, Meteor.bindEnvironment(function(err, containers) {
containers.forEach(function(containerInfo) {
stateDockerLastCheck.push(containerInfo.State);
console.log("stateDockerLastCheck" + stateDockerLastCheck.length);
});
}));
console.log("end stateDockerLastCheck");
//compare last with actual
for (var i = 0; i < stateLastCheck.length; i++) {
console.log("Size of last and docker: " + stateLastCheck.length + " et " + stateDockerLastCheck.length);
if (stateLastCheck[i].includes(stateDockerLastCheck[i])) {
console.log("no problemos")
} else {
console.log("there is a changement of state: " + stateLastCheck[i] + stateDockerLastCheck[i]);
break;
}
}
setTimeout(triggerState, 7000);
});
Then if I use my logs I can see that suddently the docker array is empty and that the loop is made again:
I think I should use some wait
but I'm really lost so if someone could help me ?
After adding some '---' in my code I noticed that the input is really strange :
I think the response for the docker is coming late and your for loop is executed before that and that is why your second array is empty
Try to use promises.
Hope this helps...
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments