Meggy
Meggy

Reputation: 1691

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?

Upvotes: 0

Views: 671

Answers (1)

Amir Saleem
Amir Saleem

Reputation: 3140

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);
}

Upvotes: 2

Related Questions