0

I have a segment code like below running in Node.js. And I find it will always goes to else condiction, howerver with masterData is not null.

getOperationDetails(req, res) {
   let sql = 'select a.*, b.s*';
       sql += ` from ${paymentSheet} a left join ${paymentHisSheet} b on a.id= b.source_id `;
       sql += ' where a.id=? ';
   func.connPool(sql, id, (err, rows, field) => {
       if (err) {
         res.json({ code: 400, message: err })
       } else {
         let masterData = [];
         let details = rows.map((row, idx) => {
          if (idx === 0) {
            masterData.push({
              id: row.id,
              name: row.name
            });
          }
          return {
            operator: row.operator_info,
            comments: row.cmt,
            status: row.sta
          }
         })
        if (masterData.length > 0 ) { 
          masterData[0].details = details;
        } else {
          console.log(sql);
          console.log(id);
          console.log('=======================');
          console.log(masterData);
        }
        res.json({ code: 200, message: 'ok', data: masterData })
      }
   })

For example, the console will show like below. Obviously masterData has value. It means 'if' condiction run before map(). Do I have to use async to wait the map() handle the data over?

allConnections:2
select a.*, b.* from payment a left join history b on a.id= b.source_id  where a.id=? 
83e588cd-9b4b-4592-ac7f-529bfaa9b231
=======================
allConnections:2
allConnections:2
[
   {
     id: '83e588cd-9b4b-4592-ac7f-529bfaa9b231',
     name: 'Jeff'
    }
 ]

My anaysis:

the rows from database should like below

83e588cd-9b4b-4592-ac7f-529bfaa9b231', 'Jeff', 'Operator Peter', 'OK', 0
83e588cd-9b4b-4592-ac7f-529bfaa9b231', 'Jeff', 'Operator Mary', 'NO', 1
83e588cd-9b4b-4592-ac7f-529bfaa9b231', 'Jeff', 'Operator Jet', 'OK', 2

or like below, means no details

83e588cd-9b4b-4592-ac7f-529bfaa9b231', 'Jeff', null, null, null

That is why I use masterData to separate. I think push() should not be taken out the map(), becasue rows maybe return nothing. Will it be like map() is over and push() is still running?

==== P.S. func.connPool====

let mysql = require('mysql');
let db = require('../configs/db');
let pool = mysql.createPool(db);

module.exports = {
  connPool (sql, val, cb) {
      pool.getConnection((err, conn) => {
        if (err) {
          console.log('Connection Error:' + err);
          cb(err, null, null);
        } else {
          console.log('allConnections:' + pool._allConnections.length);
          let q = conn.query(sql, val, (err, rows,fields) => {
          pool.releaseConnection(conn);
          if (err) {
            console.log('Query:' + sql + ' error:' + err);
          }
          cb(err, rows, fields);
        });
      }
    });
  },
7
  • there's nothing asynchronous happening inside .map so ... no Commented Nov 29, 2019 at 2:37
  • im crazy about why it will go to else branch. I got the rows result from database and try to separate the master data the deatils data. Commented Nov 29, 2019 at 2:41
  • map is synchronous and don't see async calls in your code above, there must be something missing in the surrounding code? Commented Nov 29, 2019 at 2:46
  • rows is probably an empty array Commented Nov 29, 2019 at 2:49
  • Try console.log(JSON.stringify(masterData)) rather than logging the object itself. You will no doubt find masterData to be empty at that point. It's a trap for the unwary what the logged value of an object will change as the object changes. Commented Nov 29, 2019 at 2:50

2 Answers 2

1

What I suspected is that the push operation is somehow delay because of some code that is not shown here (I am not certain yet).

I ran the following code so many times, I still could not reproduce your problem.

var rows = [
    {
        id: "123",
        name: "test",
    },
    {
        id: "123",
        name: "test",
    },
    {
        id: "123",
        name: "test",
    },
]

let masterData = [];
let details = rows.map((row, idx) => {
    if (idx === 0) {
      masterData.push({
        id: row.id,
        name: row.name
      });
    }
    return {
      id: row.id,
      name: row.name,
    }
})
if (masterData.length > 0 ) {
    console.log("in");
} else {
    console.log(masterData);
    console.log('=======================');
}

Could you try whether it goes to else or not for this code.

Sign up to request clarification or add additional context in comments.

2 Comments

@Ian Was rows passed as list type? Could you provide the code for func.connPool?
Chindanonda, iv tried, and the console shows 'in'...the logic is right. Anyway, I try to take push() out map() to have a try.
0

From this piece of code you are pushing to MasterData only the first row.id and row.name.

( that is specified in the if conditional for just the first index idx === 0 )

So if thats the case you don't need to have this push thing inside the map.

You can take that out of the map and leave the iterator to create only the details array.

You can go with:

 let details = rows.map(row => ({
     operator: row.operator_info,
     comments: row.cmt,
     status: row.sta
     })
  );


let masterData = [{ id: rows[0].id, name: rows[0].name, details }]


1 Comment

Good point, though you'd need to check rows has length > 0 first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.