1

I have some code bellow trying to get all data with its child but when pushing to array its not working can someone help me ? I sending request from sever to get the child after mapping each data for the object then push to array but always get nothing.

var query = new Promise(function (resolve, reject){
        Tcompany.find(template.query, function (err, done){
            if(!err){
                resolve(done);
            } else {
                reject("PLEASE CHECK QUERY !");
            }
        });
    });

    query.then(function (result){
        var resultSize = result.length;
        var jsonXls = [];
        var companyCounter = 0;
        while (companyCounter < resultSize){
            jsonXls.push("ASDSADADS");
            var profileData = new Promise(function(resolve, reject){
                item.profileCerts.map(function (profile, profleIndex){
                    Tprofile.find({
                        where: {npwp: profile.npwp}
                    }, function (err, done){
                        if(!err){
                            resolve(done);
                        } else {
                            reject("PLEASE CHECK QUERY !");
                        };
                    });
                });
            });
            profileData.then(function (profiles){
                jsonXls.push("ASDSADSADAS"); // NOT PUSHING THE OBJECT
            });
            companyCounter++;
        }
        console.log(jsonXls);
    });
  • Feel free to provide reasons why this *isn't* a duplicate, but it appears to me that you are trying to log the array in a line of code that will run *before* the `.push()` runs. The reason why you'd want promises in the first place is because of the asynchronous nature of the operations you are doing. If you need to wait until all of the promises have completed (and the array is fully populated) you may want to use [`Promise.all()`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise/all). – nnnnnn May 23 '17 at 03:50
  • @nnnnnn Not sure how present Question is a duplicate of linked Question, though use of `while` with `Promise` could be replaced with `Array.prototype.map()` and `Promise.all()` – guest271314 May 23 '17 at 03:55
  • my reason for me to ask the question because i dont even know the problem sorry, what do you mean by logging the array before running push can you explain little bit. – Acep Muhamad Saepuloh May 23 '17 at 03:56
  • 1
    @guest271314 - It has the same underlying issue, doesn't it? Trying to access the results of asynchronous operations before those operations complete. I agree with your suggested fix. (Although, something I just noticed in that code: it's a bit weird that `result` is only used to get a `.length`, and not for its actual data, and meanwhile `item` isn't defined at all in the code shown.) – nnnnnn May 23 '17 at 04:00
  • @nnnnnn Yes, there is a similar underlying issue. This [Answer](https://stackoverflow.com/a/43766002/) does provide a `Promise.all()` approach. The caveat relating to current approach by OP to convert to working code would be that `companyCounter` is not an array, and not used within loop, though `Array.from()` can be utilized where `resultSize` is passed as part of parameter `var jsonXls = Promise.all(Array.from({length:resultSize}).map(function() { return new Promise(function(resolve, reject) {/do stuff/})})).then(function(profiles){//array of resolved Promise values})` – guest271314 May 23 '17 at 04:08
  • Replacing `while` with `Promise.all` or `map` still not giving me the object. – Acep Muhamad Saepuloh May 23 '17 at 04:12
  • @AcepMuhamadSaepuloh If the above approach does not return expected result there are probably other issues with existing code. Are any errors logged? Have you tried including `.catch()` chained to `.then()` following `Promise.all()` ? – guest271314 May 23 '17 at 04:13
  • @guest271314 the `Promise.all()` function work fine if only single map im trying to do this `Promise.all( result.map(function (company, companyIndex){ company.profileCerts.map(function (profile, profileIndex){` any idea ?. – Acep Muhamad Saepuloh May 23 '17 at 04:42
  • @AcepMuhamadSaepuloh `Promise.all( result.map(function (company, companyIndex){ return Promise.all(company.profileCerts.map(function (profile, profileIndex){ return new Promise(function(resolve, reject){}) })}))` – guest271314 May 23 '17 at 04:48
  • guest271314 and nnnnnn thank you i finally found the solution Promise.all() work great. – Acep Muhamad Saepuloh May 23 '17 at 04:51

0 Answers0