Azure Table JavaScript issue

182 views Asked by At

I'm trying to access Azure Storage Table's with JavaScript but I'm getting the following error. Unsure why this happening, I've seen many examples online where this works. I'm typing more here as it's asking for more information but this is all I have to give.

Error getting status from the table: TypeError: tableServiceClient.getTableClient is not a function

const { TableServiceClient } = require("@azure/data-tables");
const { DefaultAzureCredential } = require("@azure/identity");

const tableName = "status";
const rowKey = "redacted";

const storageAccountName = "redacted";

// Function to get and update status from Azure Storage Table
const getStatusAndUpdate = async (updateStatus) => {
    const defaultAzureCredential = new DefaultAzureCredential();
    const tableServiceClient = new TableServiceClient(
        `https://${storageAccountName}.table.core.windows.net`,
        defaultAzureCredential
    );

    const tableClient = tableServiceClient.getTableClient(tableName);
    let statusEntity;

    if (updateStatus !== undefined) {
        // Update the status
        statusEntity = await tableClient.updateEntity({
            partitionKey: "meeting",
            rowKey: rowKey,
            status: updateStatus
        }, { mode: "Merge" });
    } else {
        // Get the status
        statusEntity = await tableClient.getEntity("meeting", rowKey);
    }

    return statusEntity;
};

app.http('httpTrigger2', {
    methods: ['GET', 'POST'],
    authLevel: 'anonymous',
    handler: async (request, context) => {
        console.log("JavaScript HTTP trigger function processed a request.");

        if (request.method === 'GET') {
            try {
                const statusEntity = await getStatusAndUpdate();
                return {
                    status: 200,
                    body: JSON.stringify({ status: statusEntity.status }),
                };
            } catch (error) {
                console.log("Error getting status from the table:", error);
                return {
                    status: 500,
                    body: "Internal server error.",
                };
            }
        } else if (request.method === 'POST') {
            const updateStatus = request.query.get("status"); // Get the 'status' parameter from URL

            if (updateStatus) {
                try {
                    await getStatusAndUpdate(updateStatus);
                    return {
                        status: 200,
                        body: "Status updated successfully.",
                    };
                } catch (error) {
                    console.log("Error updating status in the table:", error);
                    return {
                        status: 500,
                        body: "Internal server error.",
                    };
                }
            } else {
                return {
                    status: 400,
                    body: "Please pass a 'status' parameter in the URL.",
                };
            }
        }
    }
});
1

There are 1 answers

0
Venkatesan On

TypeError:tableServiceClient.getTableClient is not a function

The above error occurs due to tableserviceClient does not get tablecient function.

Instead, you can directly instantiate the Tableclient in your environment.

Here is my updated code to use.

Code:

const { TableClient } = require("@azure/data-tables");
const { DefaultAzureCredential } = require("@azure/identity");

const tableName = "xxxx";
const rowKey = "xxxxx";

const storageAccountName = "xxxxx";

const getStatusAndUpdate = async (updateStatus) => {
    const credential = new DefaultAzureCredential();
    const tableClient = new TableClient(
        `https://${storageAccountName}.table.core.windows.net`,
        tableName, credential
    );

   let statusEntity;
    if (updateStatus !== undefined) {
        // Update the status
        statusEntity = await tableClient.updateEntity({
            partitionKey: "meeting",
            rowKey: rowKey,
            status: updateStatus
        }, { mode: "Merge" });
    } else {
        // Get the status
        statusEntity = await tableClient.getEntity("meeting", rowKey);
    }

    return statusEntity;
};

app.http('httpTrigger2', {
    methods: ['GET', 'POST'],
    authLevel: 'anonymous',
    handler: async (request, context) => {
        console.log("JavaScript HTTP trigger function processed a request.");

        if (request.method === 'GET') {
            try {
                const statusEntity = await getStatusAndUpdate();
                return {
                    status: 200,
                    body: JSON.stringify({ status: statusEntity.status }),
                };
            } catch (error) {
                console.log("Error getting status from the table:", error);
                return {
                    status: 500,
                    body: "Internal server error.",
                };
            }
        } else if (request.method === 'POST') {
            const updateStatus = request.query.get("status"); // Get the 'status' parameter from URL

            if (updateStatus) {
                try {
                    await getStatusAndUpdate(updateStatus);
                    return {
                        status: 200,
                        body: "Status updated successfully.",
                    };
                } catch (error) {
                    console.log("Error updating status in the table:", error);
                    return {
                        status: 500,
                        body: "Internal server error.",
                    };
                }
            } else {
                return {
                    status: 400,
                    body: "Please pass a 'status' parameter in the URL.",
                };
            }
        }
    }
});

The above code creates a new TableClient object with the storage account name, table name, and Azure credentials. If the updateStatus argument is used, the status of the meeting in the table is updated. Otherwise, it fetches the meeting's status from the table.

The app.http function HTTP trigger responds to GET and POST requests, gets or updates a meeting's state using the getStatusAndUpdate method, and provides appropriate responses.

Reference:

Azure Tables client library for JavaScript | Microsoft Learn