首页 > 解决方案 > CosmosDB UDF 返回 5 天后的日期

问题描述

我正在尝试编写一个小的 UDF 用于测试目的,它将返回今天的日期加上 5 天,我需要这个来测试一个比较一系列日期以确保应该包含文档的小查询

文件看起来像这样

{
    "id": "12345",
    "brand": "XXX",
    "PromotionName": "Test Promo 1",
    "PromotionType": "Deal",
    "PromotionSticker": "Sticker 1",
    "StartDate": "2020-05-15T00:00:00.1212122Z",
    "EndDate": "2020-05-30T00:00:00.1212122Z",
    "Variants": [
        "0628462008001",
        "0628462008002",
        "0644324003002"
    ],
    "Stores": [
        "SE0623"
    ],
    "Users": [
        "ALL"
    ],
    "DiscountInPercent": "30",
    "RedPriceStores": null,
    "CreatedDate": "20200515",
    "CreatedBy": "SLAPI Promotions API ClientId: 123",
    "UpdatedDate": null,
    "UpdatedBy": null,
    "Consumer": "Storelens_V2",
    "_rid": "HwVmAIFaOoEBAAAAAAAAAA==",
    "_self": "dbs/HwVmAA==/colls/HwVmAIFaOoE=/docs/HwVmAIFaOoEBAAAAAAAAAA==/",
    "_etag": "\"1100092f-0000-0c00-0000-5ebe07280000\"",
    "_attachments": "attachments/",
    "_ts": 1589511976
}

所以我写了一个这样的小查询

SELECT * 
FROM c
WHERE ARRAYCONTAINS(c.Variants, '0628462008001')
AND ARRAYCONTAINS(c.Stores, 'SE0623')
AND c.StartDate <= (SELECT GetCurrentDateTime())
AND c.EndDate >= (SELECT udf.todaysDatePlus5())

我的想法是我可以使用 UDF 来测试我的查询的有效性,因为它会在未来返回一个日期以用于测试目的

我的UDF小如下

function todaysDatePlus5(){
    var date = new Date();
    var a = date.setDays(+5);
    return a;
}

但是当我运行它时,我会收到这样的错误消息

Failed to query item for container promotions: {"code":400,"body":{"code":"BadRequest","message":"Message: {\"Errors\":[\"Encountered exception while executing Javascript. Exception = TypeError: Object doesn't support property or method 'setDays'\\r\\nStack trace: TypeError: Object doesn't support property or method 'setDays'\\n   at todaysDatePlus5 (todaysDatePlus5.js:3:5)\\n   at __docDbMain (todaysDatePlus5.js:8:5)\\n   at Global code (todaysDatePlus5.js:1:2)\"]}\r\nActivityId: 9a98388f-a574-4b5e-a15c-6e48755b23b4, Request URI: /apps/ebe17a4f-1254-45f9-be9c-b6b25f776dc2/services/0d1b0155-67dd-416e-bdfa-13801f1e8a6e/partitions/886be96a-2c7f-4b54-9681-e026cd2ca707/replicas/132338964530434550p/, RequestStats: \r\nRequestStartTime: 2020-05-15T03:23:07.5903385Z, RequestEndTime: 2020-05-15T03:23:07.5903385Z,  Number of regions attempted:1\r\nResponseTime: 2020-05-15T03:23:07.5903385Z, StoreResult: StorePhysicalAddress: rntbd://cdb-ms-prod-northeurope1-fd1.documents.azure.com:14006/apps/ebe17a4f-1254-45f9-be9c-b6b25f776dc2/services/0d1b0155-67dd-416e-bdfa-13801f1e8a6e/partitions/886be96a-2c7f-4b54-9681-e026cd2ca707/replicas/132338964530434550p/, LSN: 10, GlobalCommittedLsn: 10, PartitionKeyRangeId: 0, IsValid: True, StatusCode: 400, SubStatusCode: 0, RequestCharge: 1, ItemLSN: -1, SessionToken: -1#10, UsingLocalLSN: True, TransportException: null, ResourceType: Document, OperationType: Query\r\n, SDK: Microsoft.Azure.Documents.Common/2.11.0"},"headers":{"x-ms-request-charge":1,"x-ms-documentdb-query-metrics":{}},"activityId":"9a98388f-a574-4b5e-a15c-6e48755b23b4"}

如果我像这样自己尝试UDF

select udf.todaysDatePlus5()

我收到相同的错误消息

我在这里做错了什么?它是如此简单的UDF。

标签: azure-cosmosdbazure-cosmosdb-sqlapi

解决方案


尝试以下操作:

function todaysDatePlus5(){
    var date = new Date();
    var a = date.setDate(date.getDate()+5);
    return new Date(a);
}

推荐阅读