Bonjour,

le code suivant récupère 3 arrays mais on dirait qu'il n'a pas le temps d'attendre qu'un des array soit fini pour continuer. au niveau de [I] photosB.arraycomments.push({ dans le code suivant, j'ai l'erreur :

Uncaught TypeError: photosB[i] is undefined
Je ne maîtrise pas du tout ni ne comprends les questions d'attente, de synchronicité (async, await) alors j'avais crée pour un array photosBbis, qui avait fonctionné tant que je les fusionnais, beaucoup plus tard.

Ici, j'ai voulu inserer dans photosB je resultat de mes commentaires, mais ca ne marche pas. J'essaye d'éviter de créer un photosBter.

Je voudrais que le resultat des commentaires (read_photocomments, appelé par ajax) entre bien dans l'array photosB.

J'aimerai pour ça utiliser des await et async pour peut etre que l'array photosB soit rempli une fois que j'ai le resultat de l'appel ajax vers read_photocomments.


J'essaye de ne pas vous envoyer un 'wall of code'. Vous avez 3 appels ajax dans cette fonction.

merci d'avance


Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
 
    $.ajax({
        url: 'https://website/imagesInCat_ajax.php?catID=' + catconfirm + '&minLat=' + SWlat + '&maxLat=' + NElat + '&minLng=' + SWlng + '&maxLng=' + NElng,
        type: 'post',
        dataType: 'json',
        success: function(response) {
 
            $("#slider").empty();
            photosB = [];
            photosBbis = [];
 
            var i;
 
            for (i = 0; i < response.length; i++) {
                console.log(i);
                photounit = response[i];
                var url = photounit.imgurl;
 
                //  var url = 'https://website/subsite/picture.php?/' + photounit.imgurl;
                let split = url.split('/');
                let imageid = photounit.id;
                if (photounit) {
 
                    $.ajax({
                        url: 'https://website/subsite/ws.php?format=json&method=pwg.images.getInfo&recursive=true&image_id=' + imageid,
                        type: 'post',
                        dataType: 'json',
                        success: function(response) {
 
 
                            photosBbis.push({
                                id: imageid,
                                cats: response.result.categories[0].uppercats,
                                date: response.result.date_available
 
                            });
                        }
                    });
 
 
                    photosB.push({
                        id: imageid,
                        lat: photounit.latitude,
                        lng: photounit.longitude,
                        url: url,
                        //    photo: photounit.linkurl,
 
                        photo: photounit.linkurl.replace("sq", "sq"),
                        caption: photounit.name,
                        comment: photounit.comment,
                        author: photounit.author,
                        thumbnail: photounit.linkurl.replace("sq", "2s"),
                        arraycomments: [],
                        // cat: cats2
                    });
 
                    var arraycomments = [];
 
                    $.ajax({
                        url: "../callfiles/services/read_photocomments.php",
                        method: "GET",
                        dataType: "json",
                        data: {
                            image_id: imageid
 
                              },                        xhrFields: {
                            withCredentials: true
                        },
                        success: function(response) {
                            console.log(response);
 
 
                            for (let r = 0; r < response.length; r++) {
                                photosB[i].arraycomments.push({        
                                   content: response[r][0],
                                   author: response[r][1],
 
                            });
 
 
                            }
 
                        },
                        error: function (response) {
                            console.log(response);
                            arraycomments = response;
                            console.log('comments error')
                          },
                        username: null,
                        password: null
                    });
 
                }
            };