-1

Im currently having trouble getting the output below. Can somebody tell me what is wrong with the code that prevents me from getting the following output that i want? Thanks in advance!

Code:

 // http requests
 const getJSON = require('get-json');

 function getShopLocation(postalCode) {
 // Use promise to resolve when success
 return new Promise((resolve, reject) => {
 // One Map API is used to retrieve Lat & Lon & more map details
 var oneMapURL = `https://developers.onemap.sg/commonapi/search?searchVal=${postalCode}&returnGeom=Y&getAddrDetails=Y&pageNum=1`;
 getJSON(oneMapURL, function(error, response) {

 if (response) {
  // Get only the lat
  var lat = response.results[0].LATITUDE;
  // Resolve the location details
  resolve(lat);
} else {
  reject(error);
}
});
});
}

 function init() {
    var postalCode = [ 519498, 629117, 670445, 258748, 238884 ];
    var title = ['Place A', 'Place B', 'Place C', 'Place D', 'Place E'];
    var data = [];

    var i = 0;
    for (i = 0; i < postalCode.length; ++i) {
        var lat = getShopLocation(postalCode[i]);
        data.push({ title: title[i] , lat: lat});
    }

    Promise.all(data)
    .then((results) => {
        console.log("Data Output: ", results);
    })
    .catch((e) => {
        // Handle errors here
        console.log(error);
    });
    }
    init();

Output I want:

[
  { title: 'Place A', lat: 1.322595392 },
  { title: 'Place B', lat: 1.325617641 },
  { title: 'Place C', lat: 1.325617641 },
  { title: 'Place D', lat: 1.3835802030000002 },
  { title: 'Place E', lat: 1.3061957990000002 }
]
scorezel789
  • 163
  • 2
  • 10

2 Answers2

1

As you use data with Promise.all, it should be an array of promises. And getShopLocation returns a promise for the latitude, not the actual latitude.

You probably want to use

for (let i = 0; i < postalCode.length; ++i) {
    var latPromise = getShopLocation(postalCode[i]);
    var objPromise = latPromise.then(lat => ({ title: title[i] , lat: lat}));
    data.push(objPromise);
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
1

You are pushing objects to data array which are not actally promises. You need to push actual promises to an array to use them inside Promise.all

I have made the required changes in your code to function properly, please use the below code:

 // http requests
 const getJSON = require('get-json');

 function getShopLocation(postalCode) {
   // Use promise to resolve when success
   return new Promise((resolve, reject) => {
     // One Map API is used to retrieve Lat & Lon & more map details
     var oneMapURL = `https://developers.onemap.sg/commonapi/search?searchVal=${postalCode}&returnGeom=Y&getAddrDetails=Y&pageNum=1`;
     getJSON(oneMapURL, function(error, response) {

       if (response) {
         // Get only the lat
         var lat = response.results[0].LATITUDE;
         // Resolve the location details
         resolve(lat);
       } else {
         reject(error);
       }
     });
   });
 }

 function init() {
   var postalCode = [519498, 629117, 670445, 258748, 238884];
   var title = ['Place A', 'Place B', 'Place C', 'Place D', 'Place E'];
   var data = [];

   var i = 0;
   var promiseArr = [];
   for (i = 0; i < postalCode.length; ++i) {
     var lat = getShopLocation(postalCode[i]);
     promiseArr.push(lat);
   }

   Promise.all(promiseArr)
     .then((results) => {
       console.log("Data Output: ", results);
       for (let j = 0; j < promiseArr.length; j++) {
         data.push({
           title: title[j],
           lat: promiseArr[j],
         })
       }
       console.log(data); // This is my final answer.
     })
     .catch((e) => {
       // Handle errors here
       console.log(error);
     });
 }
 init();
Jasdeep Singh
  • 7,901
  • 1
  • 11
  • 28