Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

153
Visualizações
i'm trying to create a route that returns the sum of all the values of a params in a collection

i really don't know what I'm doing wrong... it returns an array with the number of rows in the collection but all null. i followed the documentation perfectly but i'm just extremely confused

basically i have a collection in my database called mytest, and i'm trying to add the valuses in each document (token) to return when i call the route /mytest-sum


module.exports = {
  /**
   * Retrieve records.
   *
   * @return {Array}
   */

  async Sum(ctx) {
    let entities;
    if (ctx.query._q) {
      entities = await strapi.services.mytest.search(ctx.query);
    } else {
      entities = await strapi.services.mytest.find(ctx.query);
    }

    //return entities.map(entity => sanitizeEntity(entity, { model: strapi.models.mytest}));

    entities = entities.map(entity => (entity, { model: strapi.models.mytest }));

    entities = entities.map(entry => {
      entry = Object.assign(entry, {
        sumField: entry.token
      });
    });
   return entities;

  
  },
};``
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

module.exports = {
async Add(ctx) {
  let add = 0;
  let fruits = []

  // get data from database
let entities;
    if (ctx.query._q) {
      entities = await strapi.services.mytest.search(ctx.query);
    } else {
      entities = await strapi.services.mytest.find(ctx.query);
    }


    //get specific data-field from data  and map it to an array
    fruits = entities.map(entity => sanitizeEntity(entity, { model: strapi.models.mytest }).token);

    // add array values in for loop
    for (let i = 0; i < fruits.length; i++) {
      add += fruits[i];
    }
//return sum as a number 
return add;
}};
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda