ChinnarajS
ChinnarajS

Reputation: 654

Azure table storage Node JS function app return response empty

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'
                }
            }
        }
    });
}

Upvotes: 0

Views: 351

Answers (1)

Jim Xu
Jim Xu

Reputation: 23111

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

Upvotes: 1

Related Questions