0

I got a data from MySQL and push it into new Array but it when I log it. It doesn't have any data. I use for loop to get each data from DB and I don't know how to push RowDataPacket into new Array.

Or is there any way to combine two SQL into one SQL line?

router.get(`/find-users/:queryString`, function(req, res, next) {
  let queryString = req.params.queryString;
  db.query(
    `SELECT distinct userId from project WHERE keyword like "%${queryString}%"`,
    function(error, data) {
      if (error) {
        console.log(error);
      }
      // console.log(data);

      let userArray = [];
      for (let i = 0; i < data.length; i++) {
        db.query(
          `SELECT * FROM user WHERE loginId='${data[i].userId}'`,
          function(error, userData) {
            if (error) {
              console.log(error);
            } else {
              console.log("-----------------------");
              console.log(userData[0]);
              // userArray[i] = userData;
              userArray.push(userData[0]);
            }
          }
        );
      }
      console.log(`-------`);
      console.log(userArray);
      console.log(`-------`);
    }
  );
});

I have to make array like this.

[ RowDataPacket {
    loginId: '박동찬',
    displayId: '107688875506148574770',
    name: '박동찬',
    bio: 'NO BIO',
  RowDataPacket {
    loginId: 'jaagupkymmel',
    displayId: '1156051',
    name: 'Jaagup Kümmel',
    bio: 'NO BIO' }
]

But it only returns like this

Result

-------
[]
-------
writingdeveloper
  • 984
  • 2
  • 21
  • 46

3 Answers3

1
const {promisify} = require('util')
router.get(`/find-users/:queryString`, async function (req, res, next) {
    const query = promisify(db.query).bind(db);
    let queryString = req.params.queryString;
    const data = await query(`SELECT distinct userId from project WHERE keyword like "%${queryString}%"`)
    if (!data) {
        console.log("error");
    }
    // console.log(data);

    let userArray = [];
    for (let i = 0; i < data.length; i++) {
        const userData = await query(`SELECT * FROM user WHERE loginId='${data[i].userId}'`)
        if (!userData) {
            console.log("error");
        } else {
            console.log("-----------------------");
            console.log(userData[0]);
            // userArray[i] = userData;
            userArray.push(userData[0]);
        }
    }
    console.log(`-------`);
    console.log(userArray);
    console.log(`-------`);
});
Nayan Patel
  • 1,683
  • 25
  • 27
0

use this instead

function(error, userData, fields)

and you get an array like shown here https://www.w3schools.com/nodejs/nodejs_mysql_select.asp

And please read up on sql injection and node.js Preventing SQL injection in Node.js

nbk
  • 45,398
  • 8
  • 30
  • 47
0

in addition to above answer,

[{
  RowDataPacket: {
    loginId: '박동찬',
    displayId: '107688875506148574770',
    name: '박동찬',
    bio: 'NO BIO',
    RowDataPacket: {
    loginId: 'jaagupkymmel',
    displayId: '1156051',
    name: 'Jaagup Kümmel',
    bio: 'NO BIO' }
    }
}]

the json should be key value pair other it wont work.

ganesh phirke
  • 471
  • 1
  • 3
  • 12