Azure-Samples / azure-cosmos-java-sql-api-samples

Sample code for Azure Cosmos DB Java SDK for SQL API
MIT License
38 stars 70 forks source link

setPostTriggerInclude doesn't set context response? #23

Open zxm5010 opened 3 years ago

zxm5010 commented 3 years ago

Hi,

I'm trying to use setPostTriggerInclude to run a post-trigger after a document added. However, I'm getting null from context.getResponse()

Post-trigger javascript (myPostTrigger):

function myPostTrigger() {
  var context = getContext();
  var container = context.getCollection();
  var response = context.getResponse(); // item that was created

  var createdItem = response.getBody();

  // we are hitting this check
  if(!createdItem) throw new Error("No created item from hooked action.")
}

Java

        JsonNode jsonNode = new ObjectMapper().valueToTree(item);
        //  Create item using container
        List<String> postTriggers = new ArrayList<>();
        postTriggers.add("myPostTrigger");
        CosmosItemRequestOptions cosmosItemRequestOptions = new CosmosItemRequestOptions()
                .setPostTriggerInclude(postTriggers);
        logger.info("itemInJson : " + jsonNode.toString());
        CosmosItemResponse<JsonNode> response = cosmosDbContainer.getContainer().createItem(jsonNode, cosmosItemRequestOptions);

I can confirm that the createItem creates document properly without the postTrigger set.