0

I'm writing a Azure function app to query entities, I'm getting the response in the callback but when result body is set with response it still returns empty,

I am not able to return the entities as response because the process thread finishes before the response is set ,I'm not sure how to solve this I've tried setInterval but no luck

below is the code

module.exports = async function (context, req) {
    var storage = require('azure-storage')
    var storageClient = storage.createTableService(process.env.connectionString)
    var storageTableQuery = storage.TableQuery
    var query = new storageTableQuery().top(1).where('Mobile eq \'' + context.req.query.mobile + '\'')
    storageClient.queryEntities("Customers", query, null, function (error, result) {
        if (error) {
            context.res = {
                status: 200,
                body: error               
            }
        }
        else {
            context.res = {
                status: 200,
                body: result.entries,
                headers: {
                    'Content-Type': 'application/json'
                }
            }
        }
    });
}
4
  • Can you confirm if there's data matching your query? Commented May 4, 2021 at 14:18
  • yes there is data when debugging Commented May 4, 2021 at 15:18
  • Can you check if you're getting continuationToken in result? Commented May 4, 2021 at 15:32
  • continuationToken is null Commented May 4, 2021 at 15:42

1 Answer 1

1

regarding the issue, please refer to the following code

const azure = require("azure-storage");
module.exports = async function (context, req) {
  const tableSvc = azure.createTableService(AZURE_STORAGE_CONNECTION_STRING);
  const tableQuery = new azure.TableQuery()
    .top(1)
    .where("PartitionKey eq ?", "Jack");
  try {
    const result = await queryEntitiesSegmented(
      tableSvc,
      "test",
      tableQuery,
      null
    );
    context.res = {
      status: 200,
      body: result.entries,
      headers: {
        "Content-Type": "application/json",
      },
    };
  } catch (error) {
    context.res = {
      status: 200,
      body: error,
    };
  }


async function queryEntitiesSegmented(
  tableSvc,
  table,
  tableQuery,
  continuationToken
) {
  return new Promise((resolve, reject) => {
    tableSvc.queryEntities(
      table,
      tableQuery,
      continuationToken,
      (error, result) => {
        if (error) {
          reject(error);
        } else {
          resolve(result);
        }
      }
    );
  });
}

enter image description here enter image description here

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

Comments

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.