【问题标题】:Access to Json element with ' ' name (Subquery mysql nodejs)使用“”名称访问 Json 元素(子查询 mysql nodejs)
【发布时间】:2019-01-21 14:43:11
【问题描述】:

我正在使用 NodeJS 和 Mysql 在后端工作,一切都很好,但最近我遇到了一个小细节,因为添加了一个 SUBQUERY。

这是 Mysql 查询:

var GetHistoryPayments = function(code){
        var query = "SELECT DISTINCT students.student_code, " +
                        "acc.id, " + 
                        "acc.StudentCode, " + 
                        "acc.DocumentDate1, " + 
                        "acc.DocEntry, " + 
                        "acc.DocumentNumber1, " + 
                        "acc.DocTotal1, " + 
                        "acc.GrosProfit, " + 
                        "acc.SumApplied, " + 
                        "acc.DocumentDate2, " + 
                        "acc.DocumentNumber2, " + 
                        "acc.DiffDocTotalPaidToDate as total_pay, " +  
                        "acc.LicTradNum, " + 
                        "acc.created_at, " + 
                        "acc.updated_at, " +
                        "(SELECT COUNT(*) FROM sap_accounting_state aa WHERE aa.DocumentNumber1 = acc.DocumentNumber1 and aa.tipo like 'Nota%' and aa.SumApplied > 0 ) as credit_notes " +
                    "FROM sap_accounting_state_students students INNER JOIN sap_accounting_state acc ON students.student_code = acc.StudentCode " +
                    "WHERE  acc.DiffDocTotalPaidToDate = 0 " +
                            "and acc.Orden = 1 " +
                            "and acc.DocumentDate1 > '2018-06-01' " +
                            "and acc.StudentCode = '" + code + "' " +

                    "GROUP BY acc.DocumentNumber1 " +

                    "ORDER BY acc.DocumentDate1 desc";
        return query;
    }

返回的 Object 数组没问题,但是 SUBQUERY 对象真的很奇怪,它返回一个空名称('': { credit_notes: 0 })。我想 SUBQUERY 创建一个具有已知名称的新对象。 ¿如何访问'' json 元素?,如何将名称 SUBQuery 设置为:'sub': { credit_notes: 0 }?。

谢谢。

[ RowDataPacket {
    students: { student_code: '18018' },
    acc:
     { id: 3836,
       StudentCode: '18018',
       DocumentDate1: 2019-01-01T05:00:00.000Z,
       DocEntry: 74998,
       DocumentNumber1: 10042438,
       DocTotal1: 1839017,
       GrosProfit: 1839017,
       SumApplied: 0,
       DocumentDate2: null,
       DocumentNumber2: 0,
       total_pay: 0,
       LicTradNum: '79593354',
       created_at: 2019-01-18T19:48:23.000Z,
       updated_at: 2019-01-18T19:48:23.000Z },
    '': { credit_notes: 0 } },
  RowDataPacket {
    students: { student_code: '18018' },
    acc:
     { id: 3842,
       StudentCode: '18018',
       DocumentDate1: 2018-12-06T05:00:00.000Z,
       DocEntry: 72048,
       DocumentNumber1: 10039576,
       DocTotal1: 346500,
       GrosProfit: 346500,
       SumApplied: 0,
       DocumentDate2: null,
       DocumentNumber2: 0,
       total_pay: 0,
       LicTradNum: '79593354',
       created_at: 2019-01-18T19:48:23.000Z,
       updated_at: 2019-01-18T19:48:23.000Z },
    '': { credit_notes: 1 } } ... ]

【问题讨论】:

    标签: javascript mysql node.js express


    【解决方案1】:

    只需将括号作为property accessor

    var item = { '': 'foo' };
    
    console.log(item['']);

    【讨论】:

    • 效果很好。非常感谢,终于可以得到 JSON 元素了。非常感谢您的帮助。
    猜你喜欢
    • 2015-10-14
    • 1970-01-01
    • 1970-01-01
    • 2019-09-04
    • 1970-01-01
    • 2017-12-01
    • 1970-01-01
    • 2020-02-08
    • 2021-05-06
    相关资源
    最近更新 更多