Home>

I want to display an array of query execution results in Node.js in the terminal.

When I query from Node.js, "undefined" is displayed in the terminal.

undefined
app.js
// Package loading
const express = require ('express');
const app = express ();
const mysql = require ('mysql');
// Specify the folder to put CSS and image files
app.use (express.static ('public'));
// mysql connection information
const connection = mysql.createConnection ({{
  host:'localhost',
  user:'roof',
  password:'*****',
  database:'list_app'
});
// Part that receives HTTP request
app.get ('/ index', (req, res) =>{
  connection.query ('SELECT * FROM users',
  (error, results) =>{
  console.log (results);
   res.render ('index.ejs');
 });
});

// Code to start the server
app.listen (3000);
mysql>show databases;
+ -------------------- +
| Database |
+ -------------------- +
| information_schema |
| list_app |
| mysql |
| performance_schema |
| sys |
+ -------------------- +
5 rows in set (0.00 sec)
mysql>use list_app;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql>show tables;
+ -------------------- +
| Tables_in_list_app |
+ -------------------- +
| users |
+ -------------------- +
1 row in set (0.00 sec)
mysql>show columns from users;
+ ------- + --------- + -------- + ------- + --------- + -------- -------- +
Field | Type | Null | Key | Default | Extra |
+ ------- + --------- + -------- + ------- + --------- + -------- -------- +
id | int (11) | NO | PRI | NULL | auto_increment |
| name | text | YES | | NULL | |
+ ------- + --------- + -------- + ------- + --------- + -------- -------- +
2 rows in set (0.00 sec)
mysql>select * from users;
+ ---- + ----------------- +
| id | name |
+ ---- + ----------------- +
| 1 | Potatoes |
| 2 | Jagai |
| 3 | Jaga |
+ ---- + ----------------- +
3 rows in set (0.00 sec)
mysql>
What I tried

I tried to pass the value from the ⇩js file to the ejs file like this.

app.get ('/ index', (req, res) =>{
  connection.query (
    'SELECT * FROM users',
    (error, results) =>{
      res.render ('index.ejs', {users: results});
    }
  );
});
<ul>
  <% users.forEach ((item) =>{%>
       <li>
           
            <span><% = item.id%></span>
            <span><% = item.name%></span>
           
       </li>
  <%});%>
</ul>


Then, the following error will be displayed on the terminal.

TypeError: /Users/tanimurashun/Desktop/list-app/views/index.ejs:29
    27 |<!-Up to here =============->
    28 |<ul>
 >>29 |<% users.forEach ((item) =>{%>
    30 |<li>
    31 | 
    32 |<span><% = item.id%></span>
Cannot read property'forEach' of undefined

Is there anyone who can teach me? We will get back to you soon. I will reply to you as soon as it is resolved. Thank you.

  • Answer # 1

    const connection = mysql.createConnection ({{
      host:'localhost',
      user:'roof',
      password:'*****',
      database:'list_app'
    });

    It's super Yamakan, but isn't it not able to connect to the MySQL server?
    Because the usernamerootNotroofIt has become.


    JavaScript isnullOrundefinedHas no properties,
    Value.property nameWhen you try to access with the dot property name like this, it fails with an error.
    This is the main reason why it doesn't work this time.

    Then whyresultsButundefinedIt will be.
    The result from MySQL will be a normal arrayforEachShould I have a property (method)? It will be.

    Even if the result is 0nullIs possibleundefinedIs funny.
    Probably the SQL statement issuance has failed.

    To find out the cause
    First in the callback functionerrorYou should check if there is anything in the argument of.

    (error, results) =>{
        console.log (error);// add this
        console.log (results);
        res.render ('index.ejs');
      });

    Even if I follow the flow of the question text roughly, it does not seem that the SQL and table information is strange ...
    So, when I looked back, I found a trace that seems to be a Typo in the user name when running MySQL.

  • Answer # 2

    user:'roof',

    Is the mysql username "roof"?

Related articles