Ropenfold
Ropenfold

Reputation: 199

AngularJs $http request stays pending and does not return value from the database

I am currently writing a route which allows me to recieve information from a stored procudre I have in a database. I have written a request in AngularJS and a route in NodeJS but I am just recieving a pending request in the chrome Network developer window. I can see that the console.log in the NodeJs app has the data I require so it has retrieved it but there is nothing coming back in any of the console logs in the the AngularJS app.

Here is the code for the both the angularJS app and the Node App:

AnglaurJS:

checkForReplenishmentEmptyTrolley = async () => {
        LIBRIS.extensions.openLoadingModal();
        console.log('in checkForReplenishmentEmptyTrolley');
        try {
          const varPromise = await $http.get(`${LIBRIS.config.stockService}stockMovement/checkForUnattachedTrolley`)
            .then((response) => {
            console.log(response);
            // Request completed successfully
            }, (error) => {
            // Request error
            console.log(error);
            });
          console.log(varPromise.data);
          // 1. check that there are no ghost replenish - lines 1-15
          console.log('in try/catch');
          console.log('promise', varPromise);
        } catch (error) {
          console.log(error);
        }
      },

NodeJS code:

app.get(`${ROUTE}/attachTrolley`, async function(req, res){
  const newRequest = await DB.newRequest();
  console.log('we have made it to the route');
  try {
    console.log('we have made it to the Try/Catch route');
    newRequest.input();
   const record = await newRequest.execute('dbo.usp_STK_CheckForUnattachedTrolley');
    res.json(record)
    console.log(record, 'record');
  } catch (err){
    handleError(res, err);
    console.log(err);
  }
});

Upvotes: 0

Views: 869

Answers (2)

Ropenfold
Ropenfold

Reputation: 199

Solved it! I had no return statement in my route!

Upvotes: 0

Ashish Modi
Ashish Modi

Reputation: 7770

The problem is that you are doing a .then on a awaited promises and not returning anything from that. You have two choice here

  • Either return response from then so when you try to access the value here console.log(varPromise.data); it works.

  • Or remove the .then alltogather as it is not required because you are awaiting it any ways.

Basically just do this

checkForReplenishmentEmptyTrolley = async () => {
  LIBRIS.extensions.openLoadingModal();
  console.log("in checkForReplenishmentEmptyTrolley");
  try {
    const varPromise = await $http.get(`${LIBRIS.config.stockService}stockMovement/checkForUnattachedTrolley`);
    console.log(varPromise.data);
    // 1. check that there are no ghost replenish - lines 1-15
    console.log("in try/catch");
    console.log("promise", varPromise);
  } catch (error) {
    console.log(error);
  }
};

Hope this fixes your issue.

Upvotes: 1

Related Questions