简体   繁体   中英

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

I'm working in a backend with NodeJS and Mysql, everything is good but recently I've faced a small details where I'm stuck since added a SUBQUERY.

This is the Mysql Query:

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;
    }

The Object array returned is ok, but the SUBQUERY Object is really weird, it returns with a empty name ( '': { credit_notes: 0 } ). I suppose the SUBQUERY creates a new object with a known name. ¿How could access a '' json element?, how could put a name SUBQuery like: 'sub': { credit_notes: 0 } ?.

Thanks.

[ 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 } } ... ]

Just take brackets as property accessor .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM