期望JSON输出不会在node.js输出中返回

马利斯·费尔南多(Malith Fernando):

我有一个名为user_feedbacks的表,

在此处输入图片说明

对于类型,我们有4个默认值 -1建议,2请求,3损坏报告,4事件

对于状态,我们有3个默认值 -0开,1进行中,2关

我想在每种状态的每种类型下显示记录数,如[Required Output]所示

{
  "status": "success",
  "code": 200,
  "data": {
    "1"(Type): {
      "0" (status): 20, (total)
      "1": 19,
      "2": 28
    },
    "2": {
      "0": 20,
      "1": 19,
      "2": 28
    },
    "3": {
      "0": 20,
      "1": 19,
      "2": 28
    },
    "4": {
      "0": 20,
      "1": 19,
      "2": 28
    }
  }
}

下面是我的代码,

在feedback.js中

static async getFeedbackStats(opts) {
    assert.object(opts, 'opts')
    assert.func(opts.mysql_db, 'opts.mysql_db')

    const { mysql_db } = opts

    const result = mysql_db.select('type', 'status', mysql_db.raw('count(status)'))
        .from('user_feedbacks')
        .groupBy('status', 'type')

    return result
  }

在handler.js中

const assert = require('assert-plus')
const Utils = require('../common/repos/feedbacks')
const Auth = require('../common/auth')
const Logger = require('../common/debug_logger')

async function handler(event, ctx, opts) {
    Logger.info('Feedback stats handler has been invoked: %j', event)

    const auth = await Auth.authenticate(event)

    if (!auth.success){
        return { status : 'failed', code : 401}
    }

    assert('params' in auth, 'auth.params')
    assert('auth_ctx' in auth, 'auth.auth_ctx')
    assert('user_id' in auth.auth_ctx, 'auth.auth_ctx.user_id')

    Logger.info('Feedback stats handler Auth.Params: %j', auth.params)

    const result = await Utils.getFeedbackStats(opts)

    return { status: 'success', code: 200, data: result }

}

module.exports = { handler }

上面的代码返回下面的输出[Output i return]

    {
    "status":"success",
    "code":200,
    "data":[
        {
          "type":1,
          "status":0,
          "count(status)":2
        },
        {
          "type":2,
          "status":0,
          "count(status)":1
        },
        {
          "type":1,
          "status":1,
          "count(status)":1
        },
        {
          "type":1,
          "status":2,
          "count(status)":1
        }
          ]
}

任何人都可以帮助我获得上面提到的输出结果吗?

除夕:

因此,在处理程序中,您需要更改:

const result = await Utils.getFeedbackStats(opts);

return { status: 'success', code: 200, data: result }

const result = await Utils.getFeedbackStats(opts);
const processedResult = processResult(result);

return { status: 'success', code: 200, data: processedResult }

现在processedResult获取数据并返回数据。这是一个非常简单的算法,仅对数据进行混洗。比异步/等待要容易得多。

const processResult = ( { status, code, data } ) => {
    // This might be shorter to program
    const objResult = {
        "1": { "0": 0, "1": 0, "2": 0 },
        "2": { "0": 0, "1": 0, "2": 0 },
        "3": { "0": 0, "1": 0, "2": 0 },
        "4": { "0": 0, "1": 0, "2": 0 },
    };
    // augment default return
    return { 
      status, 
      code, 
      data: data.reduce((acc, { type, status, count }) => {
        return {...acc, [type]: {...acc[type], [status]: count } }; 
      }, objResult)
    };
};

// test 
const example = {
  "status":"success",
  "code":200,
  "data":[
      {
        "type":1,
        "status":0,
        "count":2
      },
      {
        "type":2,
        "status":0,
        "count":1
      },
      {
        "type":1,
        "status":1,
        "count":1
      },
      {
        "type":1,
        "status":2,
        "count":1
      }
   ]
};
console.log(processResult(example));

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Python中的OpenCV不会在相同的图像给出相同的输出

JSON解组不会在我的代码中输出,可在goplayground中使用

将Golang日志输出设置为文件不会在函数声明之外持续存在

apache-commons-csv println方法不会在输出中打印换行符

sink()不会在rmarkdown中将输出打印到文本文件

Node-config不会在Heroku中使用custom-environment-variables.json中的变量

ViewSet中的SlugField不会在JSON Webform中流行

COBOL程序不会在报告中输出明细行

Highcharter不会在Shinydashboard中自动调整输出大小

正则表达式匹配不会在perl中产生输出

knitr不会在R 3.0.2上转换xtable输出

Ruby不会在输出中读取回车符(\ r)吗?

Zend Framework 1中的foreach循环不会在json输出中返回多个结果

在输出中返回null

JavaScript中的setTimeout函数不会在正确的时间间隔输出数字

Bash脚本不会在tempfile中输出任何数据

流星模板不会在每个语句中输出数据

正则表达式不会在PHP中输出所有可能的匹配项

选择输出不会在替换变量提示前显示

尝试添加ng-app名称不会在Application中给出预期的输出

发送Tabe作为Shiny的输出不会在Shiny UI中呈现

PHP sscanf不会在文件中输出第一行,但会输出以下行

在 shell 模块中使用 ssh-keyscan 不会在 Ansible 中产生任何输出

Python 导入不会在 PHP 中提供输出

php shell_exec 命令不会在 ubuntu 中给出 python 脚本的输出

Genrule 不会在最终输出文件中输出“$@”符号

nodemailer sendEmail 不会在 Node.js 中等待

在 Tkinter 中使用 if 语句不会在单击时输出

包装函数中的输出不会在装饰器之间打印