Azure 表 JavaScript 问题

问题描述 投票:0回答:1

我尝试使用 JavaScript 访问 Azure 存储表,但收到以下错误。不确定为什么会发生这种情况,我在网上看到了很多这样的例子。我在这里输入更多内容,因为它要求提供更多信息,但这就是我必须提供的全部内容。

从表获取状态时出错:TypeError: tableServiceClient.getTableClient 不是函数

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.",
                };
            }
        }
    }
});
javascript azure-storage azure-table-storage azure-identity
1个回答
0
投票

TypeError:tableServiceClient.getTableClient 不是函数

出现上述错误是因为

tableserviceClient
没有获取到
tablecient
功能。

相反,您可以直接在您的环境中实例化

Tableclient

这是我要使用的更新代码。

代码:

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.",
                };
            }
        }
    }
});

上面的代码使用存储帐户名称、表名称和 Azure 凭据创建一个新的

TableClient
对象。如果使用
updateStatus
参数,则表中会议的状态将更新。否则,它会从表中获取会议的状态。

app.http
函数 HTTP 触发器响应 GET 和 POST 请求,使用 getStatusAndUpdate 方法获取或更新会议状态,并提供适当的响应。

参考:

适用于 JavaScript 的 Azure Tables 客户端库 |微软学习

© www.soinside.com 2019 - 2024. All rights reserved.