Search code examples
mysqlnode.jsasync-awaitpromisenode-mysql

Check IF Result from Select Query with Async NodejS in MySQL?


I've got the following code in NodeJS using an Async/Promises wrapper for Mysql ;

row_c = await db.query( 'SELECT tagid FROM tags WHERE tagname = ?', [tag1] );

How do I now check if there is a result for an IF statement?

I tried;

   if (row_c.tagid){
   ///some code
    }

But it's not picking up the conditional. How do I check if the query returned a row?


Solution

  • db.query returns an array of rows. You can do the following:

    
    row_c = await db.query( 'SELECT tagid FROM tags WHERE tagname = ?', [tag1] );
    
    if (row_c.length) {
      // if data is returned
      console.log(row_c);
    }