Return all JSON items with forr

Asked

Viewed 40 times

0

Come on, guys, I need some help. I have a Function GET http and returns an array of JSON objects and would like to return the data contained in these objects, currently it returns like this:

{
  "name": "CON001 - Consulta por CPF"
}

but I have about 10 objects that look like this and I would like it to return something like:

{
  "name": "CON001 - Consulta por CPF",
  "name": "TAX001 - Exemplo",
  "name": "OPD039 - Consulta por CNPJ"
  }

follows the used Cod:

/**
  *
  * main() será executado quando você chamar essa ação
  *
  * @param As ações do Cloud Functions aceitam um único parâmetro, que deve ser um objeto JSON.
  *
  * @return A saída dessa ação, que deve ser um objeto JSON.
  *
  */

const rp = require('request-promise');

function main(params) {
    // if (!params.name)
    // {
    //     return { message: 'Nome não encontrado.' };
    // }
    return rp({
        method: 'GET',
        uri: `http aqui...`,
        json: true,
    })
    .then(body => {
        
        for (var i = 0; i < body.recordsets[0].length; i++) {
            console.log(i);
            console.log(body.recordsets[0].length);
            var result = reult + body.recordset[i] ;
            return (result);
         }

    })
    .catch(err => {
        return err;
    });
}

  • This request is returning all items and you can’t access all of them? Everything indicates that it is a problem in your backend there that is not returning everything

  • So I took out the for and just put "Return body" and returned it here{ "output": {}, "recordset": [ { "name": "CON001 - CPF query" }, { "name": "AUX004 - Prevent Screen Lock" }, { "name": "EMAIL002 - Sending email" }, { "name": "EMAIL000 - POP3/SMTP" }, { "name": "CON001 - CPF query" }, { and etec...

  • If you make an array type global variable there, and instead of giving a Return(result), give a global var_push(result) maybe it works, try there

  • I even tried that too, but it came back : { "error": "The action Did not Produce a Valid JSON Response: ["CON001 - CPF query"]" }"

  • What is the purpose of the line var result = reult + body.recordset[i] ? does not make much sense to add these two values

  • it was removed my intention was to concatenate but it did not work so well, my real intention is to be able to return all the names properly said instead of retouching = { "name": "AUX004 - Prevent Screen Lock" } return only AUX004 - Prevent Screen Lock

Show 1 more comment

1 answer

0

You are giving Return inside the for, then it runs the first loop item and returns only the first value, puts the Return outside the for, so it will all be traversed, ah and fix that line, the result variable is misspelled var result = reult + body.recordset[i];

  • Thanks, the line has been fixed even putting the Return out it returns empty, Sainda: Results: {} Logs: ;[]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.