Azure / azure-cosmosdb-js-server

The JavaScript SDK for server-side programming in Azure Cosmos DB
MIT License
179 stars 152 forks source link

BulkImport failing with serialization error #20

Open huemansdesign opened 6 years ago

huemansdesign commented 6 years ago

I'm getting the following error when executing the BulkImport stored procedure code. I've gone through several iterations. I copied a stack overflow with the same issue unresolved. It appears the store procedures can not accept array as an input. I eventually got it to work by inputing a stringified array like below. I then had to parse the input in the store proc.

var testArr = [] for(var i=0; i<150; i++){ testArr.push({"id":"test"+i}) } var testArrStr = JSON.stringify(testArr) //passed above to store proc with below alteration

exports.storeProcedure = { id: "bulkImportArray", serverScript:function bulkImportArray(docs) { var context = getContext(); var collection = context.getCollection(); var docsToCreate = JSON.parse(docs) var count = 0; var docsLength = docsToCreate.length; if (docsLength == 0) { getContext().getResponse().setBody(0); } var totals = "" function insertDoc(){ var msg = " count=" + count+" docsLength=" +docsLength + " typeof docsToCreate[]=" + typeof docsToCreate+ " length =" + docsToCreate.length if(typeof docsToCreate[count] != 'undefined' ) {

            collection.createDocument(collection.getSelfLink(),
                docsToCreate[count],
                function (err, documentCreated) {
                    if (err){
                    // throw new Error('Error' + err.message);
                    getContext().getResponse().setBody(count + " : " + err);
                    }else{ 
                      if (count < docsLength -1) { 
                            count++;    
                            insertDoc();
                            getContext().getResponse().setBody(msg);
                        } else { 
                            getContext().getResponse().setBody(msg);
                        }
                    }
                });
             }else{ 
                 getContext().getResponse().setBody(msg);
             }    

    }
    insertDoc()
}

}

An array i.e. throws the following error

[{"id":"test0"},{"id":"test1"},{"id":"test2"},{"id":"test3"},{"id":"test4"},{"id":"test5"},{"id":"test6"},{"id":"test7"},{"id":"test8"},{"id":"test9"},{"id":"test10"},{"id":"test11"},{"id":"test12"},{"id":"test13"},{"id":"test14"},{"id":"test15"},{"id":"test16"},{"id":"test17"},{"id":"test18"},{"id":"test19"},{"id":"test20"},{"id":"test21"},{"id":"test22"},{"id":"test23"},{"id":"test24"},{"id":"test25"},{"id":"test26"},{"id":"test27"},{"id":"test28"},{"id":"test29"},{"id":"test30"},{"id":"test31"},{"id":"test32"},{"id":"test33"},{"id":"test34"},{"id":"test35"},{"id":"test36"},{"id":"test37"},{"id":"test38"},{"id":"test39"},{"id":"test40"},{"id":"test41"},{"id":"test42"},{"id":"test43"},{"id":"test44"},{"id":"test45"},{"id":"test46"},{"id":"test47"},{"id":"test48"},{"id":"test49"}]

_____error

Encountered exception while executing function. Exception = Error: The document body must be an object or a string representing a JSON-serialized object. Stack trace: Error: The document body must be an object or a string representing a JSON-serialized object. at createDocument (bulkImportArray.js:646:21) at tryCreate (bulkImportArray.js:24:12) at Anonymous function (bulkImportArray.js:37:26) at Anonymous function (bulkImportArray.js:691:29)

_____stackoverflow

https://stackoverflow.com/questions/47748684/documentdb-bulkimport-stored-proc-getting-400-error-on-array-json-issue?newreg=315ec12d8a1448b0908ddd6d28515664

aramparonikyan-scdm commented 6 years ago

The "docs" must be an array of array of params, otherwise, the procedure executor will treat them as multiple params of the procedure, not a single-array-param.