0

When trying to get all the rooms with the same app_id from my database im getting no results in Node but when I do exactly the same in phpmyadmin ,it works!

Query that works in phpmyadmin:

SELECT a.id FROM cc_app_rooms a JOIN cc_apps b ON a.app_id = b.id WHERE b.app_hash = "1234abc"

In Node the following does not work and logs for rows.affectedRows: "undefined"

function room_exists(app_hash, room) {
    console.log(app_id + " " + room)
    var rooms = [];
    var sql = 'SELECT a.id FROM cc_app_rooms a JOIN cc_apps b ON a.app_id = b.id WHERE b.app_hash = ?';
    con.query(sql, [app_hash], function(err, rows, fields) {
        if (err) throw err;
        console.log("ROWS: " + rows.affectedRows);
        rows.forEach(function(row) {
            rooms.push(row.id);
        });

    });
    return rooms.includes(room);
}

And im calling the function like this:

room_exists(1, "1234abc")

Table cc_apps:

|id|_____app_hash______|
|1 |______1234abc______|

Table cc_app_rooms:

|id|____app_id____|
|1 |_____1________|
|2 |_____1________|

Im getting 2 results in phpmyadmin like expected, but the same does not seem to work in node? I know im missing something but im new to this. The function logs the right values for room and app_hash and also no error is thrown.

daedsidog
  • 1,732
  • 2
  • 17
  • 36
xDrago
  • 1,772
  • 2
  • 20
  • 37
  • `rows.affectedRows` is only defined after performing an UPDATE or DELETE query. For number of rows, just use rows.length (see https://stackoverflow.com/a/16199905/5609233) – J. Kamans Dec 08 '18 at 16:12
  • Node.js is non render blocking. You need to use promise. Try this: `con.query(sql, [app_hash]).then(rows=>{}).catch(err=>console.log(err)) – SanSolo Dec 08 '18 at 16:20
  • The use of Promise fixed my problem. Thank you very much! – xDrago Dec 08 '18 at 18:56

0 Answers0