CosmosDB UDF 到 return 5 天后的日期
CosmosDB UDF to return a date 5 days from now
我正在尝试编写一个小的 UDF 用于测试目的,它将 return 今天的日期加上 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 来测试我的查询的有效性,因为它将 return 将来的日期用于测试目的
我的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。
尝试如下操作:
function todaysDatePlus5(){
var date = new Date();
var a = date.setDate(date.getDate()+5);
return new Date(a);
}
我正在尝试编写一个小的 UDF 用于测试目的,它将 return 今天的日期加上 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 来测试我的查询的有效性,因为它将 return 将来的日期用于测试目的
我的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。
尝试如下操作:
function todaysDatePlus5(){
var date = new Date();
var a = date.setDate(date.getDate()+5);
return new Date(a);
}