我正在学习根据以下信息编写CosmosDB存储过程
我想做的是遍历查询返回的许多文档,并找到最匹配的文档。
流程如下
文档如下
{
"id": "12345",
"brand": "XXX",
"PromotionName": "Test Promo 1",
"PromotionType": "Deal",
"PromotionSticker": "Sticker 1",
"StartDate": "2020-05-14T00: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": "YYYYY_V2",
"_rid": "HwVmAIFaOoEBAAAAAAAAAA==",
"_self": "dbs/HwVmAA==/colls/HwVmAIFaOoE=/docs/HwVmAIFaOoEBAAAAAAAAAA==/",
"_etag": "\"11005859-0000-0c00-0000-5ebe0f7e0000\"",
"_attachments": "attachments/",
"_ts": 1589514110
}
基于CosmosDB中的模板,存储过程的开始看起来像这样
// SAMPLE STORED PROCEDURE
function getFinalPromotionPrice(item, store, user) {
var collection = getContext().getCollection();
// Query documents and take 1st item.
var isAccepted = collection.queryDocuments(
collection.getSelfLink(),
'SELECT * FROM c WHERE c.StartDate <= (SELECT VALUE GetCurrentDateTime()) AND c.EndDate >= (SELECT VALUE GetCurrentDateTime())',
function (err, feed, options) {
if (err) throw err;
// Check the feed and if empty, set the body to 'no docs found',
// else take 1st element from feed
if (!feed || !feed.length) {
var response = getContext().getResponse();
response.setBody('no docs found');
}
else {
var response = getContext().getResponse();
var body = { prefix: prefix, feed: feed[0] };
response.setBody(JSON.stringify(body));
}
});
if (!isAccepted) throw new Error('The query was not accepted by the server.');
}
但是执行存储过程时出现此错误:
{“代码”:400,“正文”:{“代码”:“ BadRequest”,“消息”:“消息:{\”错误\“:[\”执行功能时遇到异常。异常= ReferenceError:未定义'prefix'\ r \ n堆栈跟踪:ReferenceError:未定义'prefix'\ n在A匿名函数(script.js:20:13)\ n在A
答案 0 :(得分:0)
您可以检查错误,期望出现“前缀”:
Exception = ReferenceError: 'prefix' is not defined
在下面的行中,您将prefix的值设置为“ prefix”,但是您尚未在代码中的任何地方声明prefix。
var body = { prefix: prefix, feed: feed[0] };
如果不需要SP主体中的前缀,请将上面的行更改为此:
var body = { feed: feed[0] };