2

This code returns an error because 'results(mysql.query)' returns undefined value. I have created a database table of 7~10 rows using mysql command shell and the database works perfectly from both workbench and command shell. is it because I have installed mysql in a wrong way? or code fault? I have tested the very same stuff in a different computer and it worked perfectly from there and suddenly it doesn't work. I didn't copy the file, just wrote/typed the same stuff from memory.

I have searched stackoverflow already and it seems everybody talks about async problem, which isn't really the one in my case.(same thing worked before)

used npm install mysql, mysql version is community 5.7

crudserver.js(main file)

const fs = require("fs"); //filesystem
const ejs = require("ejs");
const mysql = require("mysql");
const express = require("express"); //handles all the middlewares
const bodyParser = require("body-parser"); //handles "POST"

const client = mysql.createConnection({
  user:"****",
  password:"****",
  database:"testdb"
});

let app = express();
app.use(bodyParser.urlencoded({
  extended:false
}));

app.listen(52273, function(){
  console.log("server running at http://127.0.0.1:52273");
});

app.get("/",function (request,response){
  fs.readFile("crudlist.ejs","utf8",function(error,data){
    client.query("SELECT * FROM products", function(error,results){
      response.send(ejs.render(data,{
        data:results,
        mytitle:"title1",
        mytitlesub:"title2"
      }));
    });
  });
});

crudlist.ejs

 <% data.forEach(function (itm,index){ %>
 some codes....
 <% } >

and it says the 'data' is undefined.

Cannot read property 'forEach' of undefined at eval (eval at compile (F:\mycoding\node_modules\ejs\lib\ejs.js:549:12), :17:12) at returnedFn (F:\mycoding\node_modules\ejs\lib\ejs.js:580:17) at Object.exports.render (F:\mycoding\node_modules\ejs\lib\ejs.js:384:37) at Query._callback (F:\mycoding\crudserver.js:25:25) at Query.Sequence.end (F:\mycoding\node_modules\mysql\lib\protocol\sequences\Sequence.js:88:24) at F:\mycoding\node_modules\mysql\lib\protocol\Protocol.js:398:18 at Array.forEach (native) at F:\mycoding\node_modules\mysql\lib\protocol\Protocol.js:397:13 at _combinedTickCallback (internal/process/next_tick.js:73:7) at process._tickCallback (internal/process/next_tick.js:104:9)

2 Answers 2

1

If data is undefined, you most likely have an error that you decided not to handle:

// note the first parameter of callback function - "error"
client.query("SELECT * FROM products", function(error,results){

You should test whether or not the error exists, and render the results only if it doesn't.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your insight. the error contained this message: { Error: connect ECONNREFUSED 127.0.0.1:3306 at Object.exports._errnoException (util.js:1018:11) at exports._exceptionWithHostPort (util.js:1041:20) at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1086:14) -------------------- at Protocol._enqueue (F:\mycoding\node_modules\mysql\lib\protocol\Protocol.js:145:48) at Protocol.handshake (F:\mycoding\node_modules\mysql\lib\protocol\Protocol.js:52:23) at Connection.connect (F:\mycoding\node_modules\mysql\lib\Connection.js:130:18)
at Connection._implyConnect (F:\mycoding\node_modules\mysql\lib\Connection.js:461:10) at Connection.query (F:\mycoding\node_modules\mysql\lib\Connection.js:206:8) at F:\mycoding\crudserver.js:24:12 at tryToString (fs.js:456:3) at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:443:12) code: 'ECONNREFUSED', errno: 'ECONNREFUSED', syscall: 'connect', address: '127.0.0.1', port: 3306, fatal: true }
For your question, it doesn't matter what the error is. It matters that you should handle it. As for this specific error, you don't have a mysql server listening on port 3306 on your localhost.
it seems mysql connection is blocked by firewall or something. I'll be back with the fixed result.
0

problem solved: reinstalled mySQL. setup port to default 3306. I did set up my port to some custom numbers when I was installing, and it was the real cause of the problem. do not change port number from 3306 unless it is necessary.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.