1

I'm using mysql with nodeJS and creating a query. The rows I get for the query are 'undefined'. This has never happened to me before, and I'm stumped.

 // check if URL already exists in database connection.query("SELECT * FROM shorturl WHERE urlsource='?'", [url], function(error, rows, fields){ if (error) { console.log(error); } if (rows.length == 0) { console.log(rows); //prints 'undefined' insertURL(); } else { console.log("creating new entry"); printURL(); } }); 
8
  • 1
    Are you sure that it is really the console.log(rows); that prints undefined? This does not make sense, because then the rows.length would throw a cannot read property length of undefined error. Commented Sep 30, 2016 at 18:47
  • Oh, I modified the code later. Yes, I get the "cannot read property length of undefined" error first Commented Sep 30, 2016 at 19:58
  • And are you sure that console.log(error); does not log an error. You should wrap all of the code below if (error) { console.log(error); } in the else block of the if (error). Commented Sep 30, 2016 at 20:01
  • 1
    My guess is that you're getting an error because of the quotes around the placeholder in the query (... WHERE urlsource = '?'). Commented Sep 30, 2016 at 20:05
  • @robertklep Aren't those quotes necessary? Commented Sep 30, 2016 at 20:49

1 Answer 1

1

you can try this, a result from query connection NodeJS is array. Try to add rows[0] hope this will help.

if (rows[0].length == 0) { console.log(rows[0]); insertURL(); } else { console.log("creating new entry"); printURL(); } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.