gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 08:55:12 24 4
gpt4 key购买 nike

我有一个名为 user_feedbacks 的表,

enter image description here

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

对于状态,我们有 3 个默认值 - 0-开放、1-正在进行、2-关闭

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

{
"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 }

上面的代码返回下面的输出[输出我返回]

    {
"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));

关于javascript - 期望 JSON 输出不会在 node.js 输出中返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60531340/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com