node.js - Azure函数通过node.js查询Azure表中的特定日期

标签 node.js azure azure-functions odata azure-table-storage

出于某种原因,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 创建了一个实体,如下所示,

enter image description here

并且,我在 Azure 门户中的表存储的 future 30 天中创建了另一个实体,如下所示,

enter image description here

我尝试使用下面的 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;
  }
};

输出:

运行成功如下,

enter image description here

通过上述输出 URL,我在浏览器中获得了 1 天和 future 30 天的过期日期,如下所示,

enter image description here

关于node.js - Azure函数通过node.js查询Azure表中的特定日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76572096/

相关文章:

node.js - Nodejs express API 一个 sequelize(db) 实例到所有模型

javascript - 客户端 + 服务器项目的 Yeoman 设置

javascript - 无法通过 JavaScript 更新 solidity 合约的状态变量

Azure Stack TP2 安装 : Invocation of step 60. 61 失败

Azure Functions - 引用与 CLI 相同的库

javascript - 如何在 Node.js 中设置和访问类属性?

c# - 用于发送短信和电子邮件通知的 Azure 服务

azure - 如何通过 Azure Data Lake Store gen1 中的新文件触发 Azure Data Factory v2 或 Azure Databricks Notebook 中的管道

c# - Azure Functions 错误 - 无法将参数绑定(bind)到字符串类型

azure - CosmosDB 更改源扩展