3

I'm new to Cosmos DB stored procedure. As introduced, cosmos stored procedure is written with Javascript, and it's unable to debug by setting up the break point, so we'll have to use console.log(...) to watch element content inside the stored procedure. Here is one of my example, which is to retrieve an item from my collection based on the input parameter prefix (the 'id' property in my item), and using javascript-query-api

function example(prefix) {
    //not working with ','
    //console.log('input prefix: ', prefix);
    //this works with '+'
    console.log('input prefix: ' + prefix);
    var collection = getContext().getCollection();
    //cannot view what's collection represents
    console.log('collection:' + JSON.stringify(collection));
    collection.chain().filter(function (doc) {
        //un-comment below will make the console log blank...
        //console.log('inside filter prefix: ' + prefix);
        return doc.id == prefix;
    }).map(function (doc) {
        return {
            id: doc.id,
            price: doc.price
        }
    }).value();
}

Here is my console.log window

My console.log won't do a line break or working with comma ',':

enter image description here

So a summary of couple of issues I had are:

  • console.log inside the stored procedure won't work with comma - , but only works with colon - :
  • console.log won't do a line break... even I add \n inside...
  • I cannot get what's really inside the collection object even I've deserialized it, as you can see from screenshot it's shown as {\"spatial\":{}}
  • If I wanted to look into what the input parameter - prefix passing inside my filter function, when I un-comment my console.log('inside filter prefix: ' + prefix);, then my console log screen will show only an empty string somehow which is really ...
  • Adding more details for data schema, partition key and stored procedure execution detail

Data schema:

enter image description here

Stored procedure execution detail:

enter image description here

Stored procedure result and console.log:

enter image description here

enter image description here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Drex
  • 3,346
  • 9
  • 33
  • 58

1 Answers1

2
  1. console.log inside the stored procedure won't work with comma - , but only works with colon - :

Yes,actually it only does works with +,so please use + to combine the log information.

  1. console.log won't do a line break...even I add \n inside...

If you observe the output of log in the portal console, entire log info is shown as a long string. So, even though you add \n, it was identified as a part of string. You could add some special character like *** or ----- to distinguish.

  1. I cannot get what's really inside the collection object even I've deserialized it, as you can see from screenshot it's shown as {\"spatial\":{}}

Since SP is run on the server side,i believe that we can't access every object as locally sdk. Based on the statements listed here:

Stored procedures and triggers are registered for a particular collection. The collection object supports create, read, update and delete (CRUD) and query operations on documents and attachments in the current collection.

We could think more of collection as a base object for calling CRUD methods.Like a init client in the sdk which is used to invoking methods.

  1. If I wanted to look into what the input parameter - prefix passing inside my filter function, when I un-comment my console.log('inside filter prefix: ' + prefix);, then my console log screen will show only an empty string somehow which is really ...

Test your code,the inside prefix log does works.

enter image description here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jay Gong
  • 23,163
  • 2
  • 27
  • 32
  • thank you very much for your help! No.1 - No.3 makes perfect sense to me now! For No.4, it still didn't work on my side somehow...Not sure what issue caused that for now... – Drex Aug 21 '19 at 00:35
  • I've attached a screenshot of my No.4 execution result where the console log shows nothing but an empty string...Here I only commented out all my other `console.log` except the one in the `filter` function.(I've left all `console.log` there but it still behaves same..) – Drex Aug 21 '19 at 00:38
  • @KevDing I infer that the issue is related to partition key.As we know, the partition key need to be set when the store procedure is executed. My test is non-partition collection so everything is fine. Does your collection partitioned? Any data match the filter condition in your configured partition? – Jay Gong Aug 21 '19 at 01:28
  • Yes my collection is partitioned and also I've tried with including a _partition key value_ (even though I am not too certain if we have to include a partition key when execute the sproc, I assume not mandatory) when execute the proc but it's still not working... – Drex Aug 21 '19 at 12:51
  • @KevDing Would you please provide some sample data and the pk you set so that i can test on my side to find the differences between us? – Jay Gong Aug 22 '19 at 01:42
  • sure thing! I've attached some more screenshot about my data schema, partition key and sproc execution input, my pk is '/partition_key' as defined in my schema, here you'd see that my result returned successfully, but console log shows nothing inside. Thank you! – Drex Aug 22 '19 at 15:06