0
votes

I am learning to write CosmosDB stored procedures following the following information

Stored procedure docs

What I am trying to do is loop through a number of documents returned by a query and find the one that has the most exact match.

The flow is as follows

  1. Check Start and End Date to make sure its a valid documents 1.5 Check that the input VariantNo is included in the Variants array in the document
  2. Check if a user is included in the user array of the document OR if ALL is specified as a string in the array
  3. Check if a store is included in the stores array OR if ALL is specified as a string in the stores array

The document looks as follows

{
    "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
}

The beginnings of my stored procedure looks like this based on the template in 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.');
}

but I am getting this error when executing the stored procedure:

{"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

1

1 Answers

0
votes

As you can check the error its expecting the "prefix" :

Exception = ReferenceError: 'prefix' is not defined

In the below line you are setting the value of prefix as "prefix" but you have not declared prefix anywhere in the code.

var body = { prefix: prefix, feed: feed[0] };

Change the above line to this if you don't require prefix in your SP body:

var body = { feed: feed[0] };