Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

executeStoredProcedureArrayArg example does not work #16

Open
deepub opened this issue Sep 14, 2020 · 1 comment
Open

executeStoredProcedureArrayArg example does not work #16

deepub opened this issue Sep 14, 2020 · 1 comment

Comments

@deepub
Copy link
Contributor

deepub commented Sep 14, 2020

I believe the SampleStoredProcedure.executeStoredProcedureArrayArg() method's objective is to demonstrate how to pass arrays as an argument to the stored procedure. However the sproc_args list isn't being inserted into the Cosmos container.
Instead the javascript stored proc is simply creating a hard coded doc without accepting the array.

        sproc_args.add(new CustomPOJO("idA"));
        sproc_args.add(new CustomPOJO("idB"));
        sproc_args.add(new CustomPOJO("idC"));

        CosmosStoredProcedureResponse executeResponse = container.getScripts()
                .getStoredProcedure(sprocId+"ArrayArg")
                .execute(sproc_args, options);

The stored procedure ignores the input parameter array.

@deepub
Copy link
Contributor Author

deepub commented Sep 16, 2020

I was able to test stored procedure execution from the portal. The SP has an array parameter.

function createReplaceDocs(jsonArray) {

   var context = getContext();
   var container = context.getCollection();

   //validate if input is valid json
   if (typeof jsonArray === "string") {

      try {
         jsonArray = JSON.parse(jsonArray);
      } catch (e) {
         throw "Bad request, input to store procedure should be array of json string.";
      }
   } else {
      throw "Bad request, input to store procedure should be array of json string.";
   }

   var resultDocuments = [];

   jsonArray.forEach(function(jsonDoc) {

      if (jsonDoc.isUpdate != undefined && jsonDoc.isUpdate === true) {

         var accepted = container.replaceDocument(jsonDoc._self, jsonDoc, { etag: jsonDoc._etag },
             function (err, docReplaced) {
                if (err) throw new Error('Error' + err.message);
                resultDocuments.push(docReplaced);
             });

         if (!accepted) throw "Unable to update document, abort ";

      } else {

         var accepted = container.createDocument(container.getSelfLink(), jsonDoc,
             function (err, itemCreated) {
                if (err) throw new Error('Error' + err.message);
                resultDocuments.push(itemCreated);
             });

         if (!accepted) throw "Unable to create document, abort ";

      }

   });

   context.getResponse().setBody(resultDocuments)

}

The parameters passed in the portal include:
PartitionKey = "001"/string
Input parameter = [{"id": "A002", "storeNumber": "001", "docType": "Header"},{"id": "A003", "storeNumber": "001", "docType": "LineItem"},{"id": "A004", "storeNumber": "001", "docType": "LineItem"}]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant