Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

231
Vistas
Recursive CosmosDB stored procedure to return nested data without response.setBody()

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:

  • Get parent documents
  • For each parent documents call MyRecursiveFunction
  • MyRecursiveFunction gets child documents for the parent document
  • MyRecursiveFunction 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:

  • How do I return the data from MyRecursiveFunction to the calling parent function?
  • In case I want to use a continuation token and I achieve to return data to my parent function, will this way of structuring my Stored procedure limit my possibility to use a continuation token, since its basically two individual calls?
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda