Home > other >  Javascript calling API with Promise returns undefined values
Javascript calling API with Promise returns undefined values

Time:01-13

im trying to get the value of the promise but it says it is undefined everytime. Im new to JS so sorry for being so bad...

For the function being called i didnt put the whole code into the post just the necessary information that it is return 4 floats. I tested that function.

I UPDATED THE CODE:

async function getClosestSchachtInSeg(
  LON,
  LAT,
  FROMANGLE,
  TOANGLE,
  NS,
  maxCount
) {
  sqlQuery =
    "SELECT *, ST_X(ST_Transform (geom, 4326)) AS LON, ST_Y(ST_Transform (geom, 4326)) AS LAT,"  
    " ST_Transform(SCHAECHTE_OWN.geom, 4326) <-> ST_SetSRID(ST_MakePoint("  
    LON  
    ", "  
    LAT  
    "), 4326)::geometry AS dist "  
    ' FROM public."SCHAECHTE_OWN" SCHAECHTE_OWN '  
    " WHERE atan((ST_X(ST_Transform (geom, 4326)) - "  
    LON  
    ") /  (ST_Y(ST_Transform (geom, 4326)) - "  
    LAT  
    ")) > "  
    FROMANGLE  
    "*pi()/180"  
    " AND atan((ST_X(ST_Transform (geom, 4326)) - "  
    LON  
    ") /  (ST_Y(ST_Transform (geom, 4326)) - "  
    LAT  
    ")) < "  
    TOANGLE  
    "*pi()/180"  
    " AND ST_Y(ST_Transform (geom, 4326)) "  
    NS  
    " "  
    LAT  
    " ORDER BY dist LIMIT "  
    maxCount  
    ";";

  pool
    .query(sqlQuery)
    .then((results) => {
      if (results.rows.length > 0) {
        for (i = 0; i < results.rows.length; i  ) {
          var ResLon = results.rows[i].lon;
          var ResLat = results.rows[i].lat;
          var ResDist = results.rows[i].dist;

          var urlDistance =
            "http://gisdatavm01hw.versatel.local:5001/getShortestPathLonLatDistance/"  
            Math.round(LON * 1000000) / 1000000  
            "/"  
            Math.round(LAT * 1000000) / 1000000  
            "/"  
            Math.round(results.rows[i].lon * 1000000) / 1000000  
            "/"  
            Math.round(results.rows[i].lat * 1000000) / 1000000;

          //console.log(urlDistance);

          fetch(urlDistance)
            .then((response) => response.text())
            .then((body) => {
              //console.log(body);
              Dist = JSON.parse(body)["distance"];
              if (Dist) {
                return 10;
              }
            });
        }
      }
    })
    .catch((err) => console.error("Error executing query", err.stack));
}

async function getClosestSchachtOWNJson12SegNew(request, response) {
  response.setHeader("Access-Control-Allow-Origin", "*");
  response.setHeader("Access-Control-Allow-Credentials", "true");
  response.setHeader(
    "Access-Control-Allow-Methods",
    "GET,HEAD,OPTIONS,POST,PUT"
  );
  response.setHeader(
    "Access-Control-Allow-Headers",
    "Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers"
  );
  //response.setHeader("Content-Type", "application/json");

  const LON = request.params.LON;
  const LAT = request.params.LAT;
  const maxCount = request.params.maxCount;

  Promise.resolve([
    getClosestSchachtInSeg(LON, LAT, -90, -60, ">=", maxCount),
    getClosestSchachtInSeg(LON, LAT, -60, -30, ">=", maxCount),
    getClosestSchachtInSeg(LON, LAT, -30, 0, ">=", maxCount),
    getClosestSchachtInSeg(LON, LAT, 0, 30, ">=", maxCount),
    getClosestSchachtInSeg(LON, LAT, 30, 60, ">=", maxCount),
    getClosestSchachtInSeg(LON, LAT, 60, 90, ">=", maxCount),
    getClosestSchachtInSeg(LON, LAT, -90, -60, "<", maxCount),
    getClosestSchachtInSeg(LON, LAT, -60, -30, "<", maxCount),
    getClosestSchachtInSeg(LON, LAT, -30, 0, "<", maxCount),
    getClosestSchachtInSeg(LON, LAT, 0, 30, "<", maxCount),
    getClosestSchachtInSeg(LON, LAT, 30, 60, "<", maxCount),
    getClosestSchachtInSeg(LON, LAT, 60, 90, "<", maxCount),
  ]).then((values) => {
    console.log(values);
  });
}

Output is:

[
  Promise { undefined },
  Promise { undefined },
  Promise { undefined },
  Promise { undefined },
  Promise { undefined },
  Promise { undefined },
  Promise { undefined },
  Promise { undefined },
  Promise { undefined },
  Promise { undefined },
  Promise { undefined },
  Promise { undefined }
]

CodePudding user response:

You're forgetting an await on the call to the async function getClosestSchachtInSeg.

Change:

const promise1 = new Promise((resolve, reject) => {

  resolve(getClosestSchachtInSeg(LON,LAT,-90,-60,">=",maxCount));

});

promise1.then(result => {

  console.log(result);

  console.log(promise1);

  return result;

})

to:

const result = await getClosestSchachtInSeg(LON, LAT, -90, -60, ">=", maxCount);
console.log(result);

// do what ya gotta do

Also, make getClosestSchachtOWNJson12SegNew an async function (so the awaits above work — you can't use await in a non-async function).

  • Related