Home > Software design >  I'm trying to get first 3 results from MySQL database in Node.js and I'm getting this erro
I'm trying to get first 3 results from MySQL database in Node.js and I'm getting this erro

Time:02-10

Here's the code I'm using

con.query('SELECT * FROM tables', function(err, results) {
                if (err) throw err
                console.log(results[0].rawname)
                for(var i= 0; i <= 3; i  ) {
                    eval("var "   'name_'   i   ' = '   "'"   results[i].rawname   "'"   ";")
                    eval("var "   'url_'   i   ' = '   "'"   results[i].url   "'"   ";")
                    eval("var "   'creator_'   i   ' = '   "'"   results[i].creator   "'"   ';' )
                }

I'm getting this error:

TypeError: Cannot read property 'rawname' of undefined

Now I know it's a problem with the i variable since I tried logging it outside the loop with a 0 in the place of the i and it seems to work fine, how can I do it with the i variable?

CodePudding user response:

You are counting from zero till 3 included. So you try to loop over 4 results instead of the intended 3 results.

CodePudding user response:

so the problem was not in the js but in the table, so the thing is there was no results[1] since i only had 1 result, well, sorry for wasting your time.

  • Related