I'm in the process of building a stored procedure in CosmosDB. The purpose of the stored procedure is to recursively get all child documents which relates to a parent document in my collection.
The steps are as follows:
MyRecursiveFunctionMyRecursiveFunction gets child documents for the parent documentMyRecursiveFunction should return child documents to the parent.
function GetGroups(Id) {
var context = getContext();
var collection = context.getCollection();
var response = context.getResponse();
query = "MY QUERY"
var accepted = collection.queryDocuments(collection.getSelfLink(), query,
function (err, documents, responseOptions) {
if (err) {
throw new Error("Error" + err.message);
}
if (documents.length === 0) {
throw "Unable to find document"
}
for (let i = 0; i < documents.length; i++) {
MyRecursiveFunction(documents[i]) //<--- Assemble hierarchy
}
}
)
if (!accepted) {
throw "Some error happened"
}
function MyRecursiveFunction(document) {
for (let i = 0; i < document.children.length; i++) {
query = "MY QUERY TO GET DOCUMENT BY ID"
var accepted = collection.queryDocuments(collection.getSelfLink(), query,
function (err, child, responseOptions) {
if (err) {
throw new Error("Error" + err.message);
}
if (child.length === 0) {
throw "Unable to find documents"
}
return child[0] //<------ How do I return this to parent caller?
}
)
if (!accepted) {
throw "Some error happened"
}
}
}
}
The problem here is, that I'm having a hard time understanding how I can return the data which were fetched inside MyRecursiveFunction. The only way in which I seem to be able to do so, is if I do response.setBody(child[0]) - But obviously this sends a response to my API.
Using the returned accept property from collection.queryDocuments() only returns true or false
Therefore, my question boil down to the following:
MyRecursiveFunction to the calling parent function?