javascript - 访问名称为 ' ' 的 Json 元素(子查询 mysql nodejs)

标签 javascript mysql node.js express

我在使用 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;
    }

返回的对象数组没问题,但是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 } } ... ]

最佳答案

就把括号当成property accessor .

var item = { '': 'foo' };

console.log(item['']);

关于javascript - 访问名称为 ' ' 的 Json 元素(子查询 mysql nodejs),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54292319/

相关文章:

javascript - $(document).ready 相当于没有 jQuery

mysql - 如何在使用 Sequel Pro 导入期间将字符串更改为日期?

node.js - 如何将对象值保存在变量中

node.js - 使用 AudioContext 通过 NodeJs WebSockets 将 avconv 中的音频流式传输到 Chrome 中

javascript - 如何对无序数组进行深度比较?

javascript - 使用带有 react-leaflet 的 html5 视频

javascript - 在 Eval 中设置变量 (JavaScript)

javascript - 保存用户的套接字以在不同页面上使用

mysql - Crystal Reports 和 MySQL - 无法使用左外连接

mysql - 通过MYSQL从右边读出最后X个字母