Bonjour,
Dans un objet javascript, j'ai une variable dans laquelle je charge un geojson via une requête ajax.
Une fois que cette requête est terminée, je lance à nouveau une requête ajax sur chaque élément pour aller ajouter une propriété supplémentaire à l'élément.
voici le code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
var this.dataPoint;
this.point = function() {
return $.ajax({
type: "GET",
url: 'scripts/gpsPoints.php?u=' + that.userID + '&start=' + that.datetimeStart + '&stop=' + that.datetimeStop
});
}
this.pollution = function(x, y) {
return $.ajax({
type: "GET",
url: 'http://localhost:8080/thredds/ncss/dataThredds/mync.nc?req=station&var=NO2&latitude='+y+'&longitude='+x,
dataType: 'xml',
});
}
this.recup = function() {
that.point().done(function(data){
that.dataPoint = data;
for (var i = 0; i < that.dataPoint.features.length; i++) {
var x = that.dataPoint.features[i].geometry.coordinates[0];
var y = that.dataPoint.features[i].geometry.coordinates[1];
console.log(i);
that.pollution(x, y).done(function(xml){
//console.log(i);
that.dataPoint.features[i].properties.NO2 = $(xml).find("data[name='NO2']")[0].firstChild.nodeValue;
});
}
});
} |
Mais dans le résultat de la requête pollution (that.pollution(x, y).done(function(xml){), l'indice i est toujours égal à 1677 (console.log(i) mis en commentaire, 1677 est la taille du geojson dataPoint).
Mais comme les indices vont de 0 à 1676, j'ai l'erreur :
Uncaught TypeError: Cannot read property 'properties' of undefined
au moment où j'affecte la valeur à that.dataPoint.features[i].properties.NO2
Comment faire pour 'diffuser' l'indice i correctement et que ce soit l'élément i du tableau dataPoint qui soit mis à jour avec le résultat du ième lancement de la requête pollution ?
Merci,
Nico
Partager