出于某种原因,Azure Nodejs 中通过 odata 执行的以下代码不会返回数组中的任何数据。示例日期格式应类似于 2023-06-29T08:38:27.92Z
我想要得到的是 future 1 天和 30 天的日期
const response30Days = await client.listEntities({
queryOptions: { filter: odata`expirationDate eq datetimeoffset'${getFutureDate(30).toISOString()}'` }
});
const entities30Days = response30Days.items || [];
const response1Day = await client.listEntities({
queryOptions: { filter: odata`expirationDate eq datetimeoffset'${getFutureDate(1).toISOString()}'` }
});
const entities1Day = response1Day.items || [];
context.log("Entities with expirationDate exactly 30 days in the future:");
entities30Days.forEach(entity => {
context.log(`PartitionKey: ${entity.partitionKey}, RowKey: ${entity.rowKey}, Entity: ${JSON.stringify(entity)}`);
});
context.log("Entities with expirationDate exactly 1 day in the future:");
entities1Day.forEach(entity => {
context.log(`PartitionKey: ${entity.partitionKey}, RowKey: ${entity.rowKey}, Entity: ${JSON.stringify(entity)}`);
});
function getFutureDate(days) {
const date = new Date();
date.setDate(date.getDate() + days);
return date;
}
最佳答案
首先,我在Azure门户中的表存储的 future 创建了一个实体,如下所示,
并且,我在 Azure 门户中的表存储的 future 30 天中创建了另一个实体,如下所示,
我尝试使用下面的 Azure Functions Node js 代码来获取 future 1 天和 30 天的表存储实体。
代码:
module.exports = async function (context, req) {
const { TableServiceClient, AzureNamedKeyCredential, TableClient } = require("@azure/data-tables");
const account = "<storage-account-name>";
const accountKey = "<account-key>";
const connestring = "<connec-string>";
const tableName = "<table-name>";
const credential = new AzureNamedKeyCredential(account, accountKey);
const serviceClient = new TableServiceClient(`https://<storage-account-name>.table.core.windows.net`, credential);
try {
const futureExpirationDates = await getFutureExpirationDates();
context.res = { body: futureExpirationDates };
} catch (error) {
context.res = { status: 500, body: "An error occurred: " + error.message };
}
async function getFutureExpirationDates() {
const partitionKey = "key";
const tableClient = new TableClient(`https://<storage-account-name>.table.core.windows.net`, tableName, credential);
const currentDate = new Date();
const oneDayFromNow = new Date(currentDate.getTime() + 24 * 60 * 60 * 1000);
const thirtyDaysFromNow = new Date(currentDate.getTime() + 30 * 24 * 60 * 60 * 1000);
const queryOptions = {
queryOptions: {
filter: `expirationDate eq datetime'${oneDayFromNow.toISOString()}' or expirationDate eq datetime'${thirtyDaysFromNow.toISOString()}'`
}
};
const entities = [oneDayFromNow, thirtyDaysFromNow];
for await (const entity of tableClient.listEntities(queryOptions)) {
entities.push(entity.expirationDate);
}
return entities;
}
};
输出:
运行成功如下,
通过上述输出 URL,我在浏览器中获得了 1 天和 future 30 天的过期日期,如下所示,
关于node.js - Azure函数通过node.js查询Azure表中的特定日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76572096/