I have a database trigger in my cosmos db which is supposed to be executed on delete operation on my collection.
string triggerid = "trg-UpdateUserAssignedLocations-onDelete-Location";
string idtodelete = "9ff71836-bc95-489e-8cf0-110f4f01e50c";
var partitionKey = new PartitionKey("UserLocation");
await this.container.DeleteItemAsync<Location>(
idtodelete,
partitionKey,
new ItemRequestOptions() {
PostTriggers=new List<string>() { triggerid },
}
);
Due to some reason the trigger is not getting executed or its getting errored out. How can I troubleshoot this? Is there any way we can provide console.log() inside triggers?
Here is how I created my trigger.
function updateUserAssignedLocationsOnFacilityDeletion(){
var context = getContext();
var request = context.getRequest();
var collection = context.getCollection();
var itemToDelete = request.getBody();
if(itemToDelete.Type=='UserLocation' && itemToDelete.SystemName=='Facility'){
id = itemToDelete.id;
var isAccepted = collection.queryDocuments(
collection.getSelfLink(),
'SELECT VALUE c FROM c JOIN a IN c.AllowedLocations WHERE a.Id = ''+id+'' AND c.Type='User'',
function (err, feed, options) {
if (err) throw err;
if (!feed || !feed.length) {
var response = getContext().getResponse();
//response.setBody('no docs found');
}
else {
if(feed!=null){
feed.forEach((currentValue, index, arr)=>{
var arr = currentValue.AllowedLocations.filter(item => item.Id !== id)
currentValue.AllowedLocations = arr;
var accept = collection.replaceDocument(currentValue._self,
currentValue, function(err, itemReplaced) {
if(err) throw "Unable to update user, abort";
});
if(!accept) throw "Unable to update user, abort";
});
}
}
}
);
}
}