簡體   English   中英

訪問具有“ ”名稱的 Json 元素(子查詢 mysql nodejs)

[英]Access to Json element with ' ' name (Subquery mysql nodejs)

我正在使用 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 創建一個具有已知名稱的新對象。 ¿How can access a '' json element?,如何將名稱 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 } } ... ]

只需將括號作為屬性訪問器

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM