1

I am working on lambda function using node js. I calling a function which return a promise on then I am calling a promiseAll. My first function gets a row form DB based on ID, if row is not present I want to return error response and if present it should call promiseAll.

Following is my code:

return getDetailsOfRow(id).then(function() {
    return Promise.all([
        getDetailsOfA(query1), 
        getDetailsOfB(query2), 
        getDetailsOfC(tripLocationsQuery)
    ]).then(function(values) {
        return combineResults();
    })
});

function getDetailsOfRow(trip) {
    return new Promise((resolve, reject) => {
        con.query(query, id, (err, results, fields) => {
            if (err) {
                reject(err);
            }
            if (results.length < 1) {
                createErrorResponse();
            } else {
                //get the set column values to use for other function   
            }
        });
    });
}

function createErrorResponse() {
    return new Promise((resolve, reject) => {
        var response = {
            "isBase64Encoded": false,
            "statusCode": 404,
            "headers": {
                "Content-Type": "text/html"
            },
            "body": "Invalid Key"
        };
        resolve(response);
    });
}

promise inside createErrorResponse method is not getting called for lambda function. I am not getting any response from a lambda function. I didn't find any solution. Is it because I will br returning a promise inside a promise if call createErrorResponse() from getDetailsOfRow() ?

2 Answers 2

1

You don't need to wrap createErrorResponse function in a promise.

Add the createErrorResponse() in resolve or reject and add catch block in getDetailsOfRow to catch the error.

Try this


return getDetailsOfRow(id).then(function() {
  return Promise.all([
    getDetailsOfA(query1),
    getDetailsOfB(query2),
    getDetailsOfC(tripLocationsQuery)
  ]).then(function(values) {
    return combineResults();
  }).catch( error => {
     return error;
  });
});

function getDetailsOfRow(trip) {
  return new Promise((resolve, reject) => {
    con.query(query, id, (err, results, fields) => {
      if (err) {
        reject(err);
      }
      if (results.length < 1) {
        reject(createErrorResponse());
      } else {
        //get the set column values to use for other function
      }
    });
  });
}

function createErrorResponse() {
  return {
    isBase64Encoded: false,
    statusCode: 404,
    headers: {
      "Content-Type": "text/html"
    },
    body: "Invalid Key"
  };
}

Sign up to request clarification or add additional context in comments.

3 Comments

I am not getting any error but I am not getting any response also
Sohail I can see { "errorType": "Error", "errorMessage": "[object Object]", "stack": [ "Error: [object Object]", " at _homogeneousError (/var/runtime/CallbackContext.js:13:12)", " at postError (/var/runtime/CallbackContext.js:30:51)", " at done (/var/runtime/CallbackContext.js:57:7)", " at fail (/var/runtime/CallbackContext.js:69:7)", " at /var/runtime/CallbackContext.js:105:16", " at process._tickCallback (internal/process/next_tick.js:68:7)" ] } error in cloud watch logs
Console log in the catch block and see what is the error.
0

I see two problems here

  • You are wrapping the code for createErrorResponse in promise which is not required.
  • You are not resolving in getDetailsOfRow method so there is no way to fulfill the promise.

The code should look like this

return getDetailsOfRow(id).then(function() {
  return Promise.all([
    getDetailsOfA(query1),
    getDetailsOfB(query2),
    getDetailsOfC(tripLocationsQuery)
  ]).then(function(values) {
    return combineResults();
  }).catch( error => {
     return error;
  });
});

function getDetailsOfRow(trip) {
  return new Promise((resolve, reject) => {
    con.query(query, id, (err, results, fields) => {
      if (err) {
        reject(err);
      }
      if (results.length < 1) {
        reject(createErrorResponse());
      } else {
        resolve(results); // it should be resolved.
      }
    });
  });
}

function createErrorResponse() {
  return {
    isBase64Encoded: false,
    statusCode: 404,
    headers: {
      "Content-Type": "text/html"
    },
    body: "Invalid Key"
  };
}

3 Comments

I am resolving the promise with result I get in getDetailsOfRow
can see { "errorType": "Error", "errorMessage": "[object Object]", "stack": [ "Error: [object Object]", " at _homogeneousError (/var/runtime/CallbackContext.js:13:12)", " at postError (/var/runtime/CallbackContext.js:30:51)", " at done (/var/runtime/CallbackContext.js:57:7)", " at fail (/var/runtime/CallbackContext.js:69:7)", " at /var/runtime/CallbackContext.js:105:16", " at process._tickCallback (internal/process/next_tick.js:68:7)" ] } error in cloud watch logs
it is hard to undersrtand from this. can you stringify errorMessage?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.