我正在尝试创建一条路线 returns 集合中参数的所有值的总和

i'm trying to create a route that returns the sum of all the values of a params in a collection

我真的不知道我做错了什么...它return是一个数组,其中包含集合中的行数,但全部为空。 我完全遵循了文档,但我非常困惑

基本上我的数据库中有一个名为 mytest 的集合,当我调用路由 /mytest-sum[=12= 时,我试图将每个文档(令牌)中的值添加到 return ]


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;

  
  },
};``
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;
}};