DocumentDB resource with specified id already exists when running a pre-trigger on create -


in documentdb i've create pre trigger on create operation. trigger code following

function createblock() {     var collection = getcontext().getcollection();     var request = getcontext().getrequest();     var doctocreate = request.getbody();      if (doctocreate.documenttype)     {         var query = "select top 1 a.blocksequence order a.blocksequence desc";          var isaccepted = collection.querydocuments(collection.getselflink(), query, function (err, feed, options) {             if (err)                 throw err;              if (!feed)                 throw new error("failed find document.");              if (feed.length)             {                 doctocreate.blockcode += (feed[0].blocksequence + 1);                 doctocreate.blocksequence = feed[0].blocksequence + 1;             }             else             {                 doctocreate.blockcode += "1";                 doctocreate.blocksequence = 1;             }              var isaccepted = collection.createdocument(collection.getselflink(), doctocreate);              if (!isaccepted)                 throw new error("the call createdocument returned false.");           });     }     else         throw new error("documenttype property required.");      if (!isaccepted)         throw new error("the call querydocuments returned false."); } 

the trigger executed line above var isaccepted = collection.createdocument(collection.getselflink(), doctocreate);.

when var isaccepted = collection.createdocument(collection.getselflink(), doctocreate); executed, error thrown message: {"errors":["resource specified id or name exists"]}

i've checked , no documents same id of new document stored collection.

you shouldn't try write in trigger. should modify body or throw error. in modifying body, change document created. in throwing error abort operation.

so instead of:

var isaccepted = collection.createdocument(collection.getselflink(), doctocreate); 

do:

return request.setbody(doctocreate); 

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -