I was working on simple command which checks if there is a specific data in MySql table. First, I tried console.log(result):
const checksql = "SELECT * FROM web_editor_data where name='somename'";
connection.query(checksql, (err, result, fields) => {
console.log(checksql);
}
It logged [].
Therefore, I tried:
const checksql = "SELECT * FROM web_editor_data where name='somename'";
connection.query(checksql, (err, result, fields) => {
console.log(checksql == []);
}
However, it logged false.
Is there anything wrong with my code? Or is there a more effective alternative for this?
Thanks in advance.
checksql == []
This is a great guess, unfortunately in Javascript when comparing arrays and objects, they are not compared by value (what they contain) but if the are literally the same thing.
So checksql is only identical to checksql, which is why this doesn't work. [] is a different array, and if you add an item to [] it doesn't automatically get added to checksql too.
The typical way to just see if something is empty, is by counting the number of items.
You can do this with the length property:
checksql.length === 0; // If true, array is empty