0

I need to access the output of a query function outside of that function.

Could anyone help me, please?

This is my code

var params = {
    TableName: "myTbl",
    ProjectionExpression: "#user_id, level, info.email",
    KeyConditionExpression: "#user_id = :ui and level= :l",
    ExpressionAttributeNames: {
        "#user_id": "user_id"
    },
    ExpressionAttributeValues: {
        ":ui": "s123234",
        ":l": "simple",
    }
};

function searchDatabase(params) {
    docClient.query(params, function (err, data) {
        if (err) {
            console.log("Error: ", JSON.stringify(err, null, 2));
        } else {
            console.log("Results: ", data);
        }
    });
};

data = searchDatabase(params)
console.log(data);

I need to get the results for the second console.log. But only the first one (that one inside the function) work properly.

Tamir Klein
  • 3,514
  • 1
  • 20
  • 38
  • 2
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Chris White May 10 '19 at 15:00
  • Thank you so much. I want to modify the code with the callback solution, but I have problems with that. Could you please help me to make a correct use of "callback" in my code? – user11481738 May 17 '19 at 08:10

0 Answers0