执行 CosmosDB 存储过程时出错
Error when executing CosmosDB stored procedure
我正在学习根据以下信息编写 CosmosDB 存储过程
我想做的是遍历查询返回的大量文档并找到最匹配的文档。
流程如下
- 检查开始日期和结束日期以确保其有效文件
1.5 检查输入的VariantNo是否包含在文档中的Variants数组中
- 检查用户是否包含在文档的用户数组中,或者是否将 ALL 指定为数组中的字符串
- 检查商店是否包含在商店数组中,或者是否将 ALL 指定为商店数组中的字符串
文档如下所示
{
"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.');
}
但我在执行存储过程时遇到此错误:
{"code":400,"body":{"code":"BadRequest","message":"Message: {\"Errors\":[\"Encountered exception while executing function. Exception = ReferenceError: 'prefix' is not defined\r\nStack trace: ReferenceError: 'prefix' is not defined\n at Anonymous function (script.js:20:13)\n at A
因为你可以检查它期望的错误 "prefix" :
Exception = ReferenceError: 'prefix' is not defined
在下面的行中,您将 prefix 的值设置为 "prefix",但是您没有在代码中的任何地方声明 prefix。
var body = { prefix: prefix, feed: feed[0] };
如果您的 SP 正文中不需要前缀,请将上面的行更改为:
var body = { feed: feed[0] };
我正在学习根据以下信息编写 CosmosDB 存储过程
我想做的是遍历查询返回的大量文档并找到最匹配的文档。
流程如下
- 检查开始日期和结束日期以确保其有效文件 1.5 检查输入的VariantNo是否包含在文档中的Variants数组中
- 检查用户是否包含在文档的用户数组中,或者是否将 ALL 指定为数组中的字符串
- 检查商店是否包含在商店数组中,或者是否将 ALL 指定为商店数组中的字符串
文档如下所示
{
"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.');
}
但我在执行存储过程时遇到此错误:
{"code":400,"body":{"code":"BadRequest","message":"Message: {\"Errors\":[\"Encountered exception while executing function. Exception = ReferenceError: 'prefix' is not defined\r\nStack trace: ReferenceError: 'prefix' is not defined\n at Anonymous function (script.js:20:13)\n at A
因为你可以检查它期望的错误 "prefix" :
Exception = ReferenceError: 'prefix' is not defined
在下面的行中,您将 prefix 的值设置为 "prefix",但是您没有在代码中的任何地方声明 prefix。
var body = { prefix: prefix, feed: feed[0] };
如果您的 SP 正文中不需要前缀,请将上面的行更改为:
var body = { feed: feed[0] };