Build a simple app using Node JS and MySQL.

Atauba Prince on June 23, 2018

Hello world!. In this tutorial, we are going to build a simple CRUD application using Node JS and MySQL. What we will build? The im... [Read Full]
markdown guide
 

Hi Atauba,

I followed your tutorial and have the following error. Do you have any clues as to why this is so:

C:\Users\PC\OneDrive\Coding\JS\node-mysql-crud-app>nodemon app.js
[nodemon] 1.18.10
[nodemon] to restart at any time, enter rs
[nodemon] watching: .
[nodemon] starting node app.js
C:\Users\PC\OneDrive\Coding\JS\node-mysql-crud-app\routes\index.js:11
title: Welcome to Socka | View Players
^

SyntaxError: Unexpected identifier
at new Script (vm.js:79:7)
at createScript (vm.js:251:10)
at Object.runInThisContext (vm.js:303:10)
at Module._compile (internal/modules/cjs/loader.js:656:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
at Module.load (internal/modules/cjs/loader.js:598:32)
at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
at Function.Module._load (internal/modules/cjs/loader.js:529:3)
at Module.require (internal/modules/cjs/loader.js:636:17)
at require (internal/modules/cjs/helpers.js:20:18)
[nodemon] app crashed - waiting for file changes before starting...
CTerminate batch job (Y/N)? y

 

I have the same error on line 11. Do you have any hints or a solution? Thank you for a short reply.

 

Try adding the "" for the title values wherever this error shows.

 

Hi, sorry for the late reply, check if all brackets/braces have been closed and ensure you added the comma after the title key in the res.render function of the index.js file.

 

I have created a node & mysql app following this tutorial. It's all good but when i open the image, the image is not opening. all images size is 0 bytes only. Seeing broken image in browser too.

 

Sorry about that, I think these may be the possible reasons

  1. The SQL statement
  2. The image url. Check the console of your browser if any errors exist
 
 

Hello

I change the first line by :
fs.writeFile(public/assets/img/${image_name},uploadedFile.data, function (err) {...

And it's work for me

 

Hi Atauba!
Great guide, very good!
I have this error:
{"errno":-4058,"code":"ENOENT","syscall":"open","path":"C:\WebTest\public\assets\img\photo1.jpeg"}
when I add new PLAYER.
In img folder I've copied three photo .jpeg
Where is the problem?

 
 

adding players is not working.It is not giving any error.
I tried to add player, once i open the add player page/form AND some player information it shows this after submitting
{
"errno":-2,
"code":"ENOENT",
"syscall":"open",
"path":"public/assets/img/user.png"
}
and when i went back to homepage no player is added.

 

This could be from the addPlayer.js function or the path to the store the image does not exist, kindly crosscheck and try again.

 

I don't know if Atauba is aware of this but there is a simple error if you direct copy the instructions. There may need to be quotes added to line 12 in player.js. They correct line might show: title: 'Welcome to Socka | Add a new player',

There were about five lines akin to that.

Other than that it works really well! =)

 

Hi!
I get error after clicking 'Add a Player'. Here is the output:

ReferenceError: /home/corny/dev/node/node-msql-crud-app/views/add-player.ejs:7
5| <% } %>
6|

7| <% if (player) { %>
8|


9|
10|

player is not defined
at eval (eval at compile (/home/corny/dev/node/node-msql-crud-app/node_modules/ejs/lib/ejs.js:618:12), :34:8)
at returnedFn (/home/corny/dev/node/node-msql-crud-app/node_modules/ejs/lib/ejs.js:653:17)
at tryHandleCache (/home/corny/dev/node/node-msql-crud-app/node_modules/ejs/lib/ejs.js:251:36)
at View.exports.renderFile as engine
at View.render (/home/corny/dev/node/node-msql-crud-app/node_modules/express/lib/view.js:135:8)
at tryRender (/home/corny/dev/node/node-msql-crud-app/node_modules/express/lib/application.js:640:10)
at Function.render (/home/corny/dev/node/node-msql-crud-app/node_modules/express/lib/application.js:592:3)
at ServerResponse.render (/home/corny/dev/node/node-msql-crud-app/node_modules/express/lib/response.js:1008:7)
at addPlayerPage (/home/corny/dev/node/node-msql-crud-app/routes/player.js:5:13)
at Layer.handle as handle_request

================
Can you help me?

BR,
Corny

 

Hello, sorry about the error, kindly check the add-player.ejs and edit-player.ejs files to confirm everything checks out and then try again.

 

My god, I switched files contents... I am an idiot. Thank you for help and great tutorial.

BR,
Corny

 

When upload image, in the folder /assets/img files was create, but empty (0 bytes).

Can you help me? Additional permission was required for the folder /assets/img?

Best Rgs.

 

Hello, sorry for the delayed reply. No additional permission was required for the folder and kindly compare this file with yours to ensure there are no errors in the addPlayer function.

 

it does not throw errors:

uploadedFile.mv(public/assets/img/${image_name}, (err ) => {
if (err) {
return res.status(500).send(err);
}

 

there are no differences.
the files are identical.
the files are created but 0 bytes.
Any suggestions?

I have no suggestions yet, but is it working?

Following the post of Al-mck....

I change the first line by :
fs.writeFile(public/assets/img/${image_name},uploadedFile.data, function (err) {...

and now it's working fine.

Best Rgs.

 

Great guide, very useful.

I'm looking to create something similar using this as a starting point but I'm struggling to remove the image upload requirement, it's not something I need. Could you give any advice on how to remove this requirement? I've encountered a few issues when trying to change this code.

Thanks in advance

Never mind, I figured it out :)

 

Okay but note that the code is susceptible to SQL injection, I have been quite lazy about fixing and updating it on the repo 😩.

Anyways, thanks for the kind words.

 

I'm new to this and I get an awful error. Did it from scratch and cloned your repo but still got this in my terminal:

..../node-mysql-crud-app/node_modules/mysql/lib/protocol/Parser.js:80
throw err; // Rethrow non-MySQL errors

^

any clues!?

 

mj0lken this error suggests that your database credentials in app.js are not correct. Check them. I realise you asked this question many months ago and have probably fixed it by now.

 

Hello, could you please send a picture of your console with the error so I can know where the error is coming from

 

SyntaxError: Unexpected identifier
at Module._compile (internal/modules/cjs/loader.js:721:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:690:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object. (D:\Programming\node js\node-mysql-crud-app\app.js:9:78)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)

D:\Programming\node js\node-mysql-crud-app>

I got stuck here..i need the solution very urgently..

btw thanks for your great guide

 

Hi Atauba,

I'm tring to learn node js, Your artical is so nice and esy to understan but i have the following error please help me to fix this issue.

Thanks

/var/www/html/node-mysql-crud-app/routes/index.js:11
title: Welcome to Socka | View Players,
^

SyntaxError: Unexpected identifier
at Module._compile (internal/modules/cjs/loader.js:760:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)
at Module.load (internal/modules/cjs/loader.js:685:32)
at Function.Module._load (internal/modules/cjs/loader.js:620:12)
at Module.require (internal/modules/cjs/loader.js:723:19)
at require (internal/modules/cjs/helpers.js:14:16)
at Object. (/var/www/html/node-mysql-crud-app/app.js:8:23)
at Module._compile (internal/modules/cjs/loader.js:816:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)
at Module.load (internal/modules/cjs/loader.js:685:32)

 

I found the answer.. Have to modified title like below.

Thanks

res.render('index.ejs', {
title: 'Welcome to Socka | View Players',
players: 'result'
});

 

Hi, I have a problem:

TypeError: C:\players\node-mysql-crud-app\views\index.ejs:18
16|

17|                 <tbody>

18| <% players.forEach((player) => { %>

19|                         <tr>

20|                             <th scope="row"><%= player.id %></th>

21|                             <td><img src="/assets/img/<%= player.image %>" class="rounded-circle player-img" alt=""></td>

players.forEach is not a function
at eval (eval at compile (C:\players\node-mysql-crud-app\node_modules\ejs\lib\ejs.js:618:12), :28:16)
at returnedFn (C:\players\node-mysql-crud-app\node_modules\ejs\lib\ejs.js:653:17)
at tryHandleCache (C:\players\node-mysql-crud-app\node_modules\ejs\lib\ejs.js:251:36)
at View.exports.renderFile as engine
at View.render (C:\players\node-mysql-crud-app\node_modules\express\lib\view.js:135:8)
at tryRender (C:\players\node-mysql-crud-app\node_modules\express\lib\application.js:640:10)
at Function.render (C:\players\node-mysql-crud-app\node_modules\express\lib\application.js:592:3)
at ServerResponse.render (C:\players\node-mysql-crud-app\node_modules\express\lib\response.js:1016:7)
at Query.db.query (C:\players\node-mysql-crud-app\routes\index.js:10:17)
at Query. (C:\players\node-mysql-crud-app\node_modules\mysql\lib\Connection.js:525:10)

How can I fix it?

BR,
Aleksandra

 

but ok, it was no question. it was just a stupid error in my index.ejs file. Now it works! :)

 
 

Great Guide Prince. Though am a bit stuck:
title: Welcome to Socka | Add a new player
^

SyntaxError: Unexpected identifier
at new Script (vm.js:80:7)
at createScript (vm.js:274:10)
at Object.runInThisContext (vm.js:326:10)
at Module._compile (internal/modules/cjs/loader.js:664:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
//Am still getting the above error even after using:

res.render('index.ejs', {
title: "Welcome to Socka | View Players"
,players: result

 

Please help with erro .

// 20190518173531
// localhost:5000/add

{
"errno": -4058,
"code": "ENOENT",
"syscall": "open",
"path": "D:\Sites\Crud3\public\assets\img\syednayab.jpeg"
}

 

Hi Atauba,

Thanks for sharing this - it's a helpful example that I'm adapting as a loadtest target.

I noticed that a few changes might help mitigate SQL injection issues. If I get time, I might come back with a pull request...

 

A VERY USEFUL POST, BUT I GET THIS ERROR EACH TIME TRY TO ADD A PLAYER :(

PD: I ONLY CHANGE "NUMBER" FOR "NUMBERO" ITS THE SAME

{"code":"ER_TRUNCATED_WRONG_VALUE_FOR_FIELD","errno":1366,"sqlMessage":"Incorrect integer value: 'undefined' for column 'numbero' at row 1","sqlState":"HY000","index":0,"sql":"INSERT INTO players (first_name, last_name, position, numbero, image, user_name) VALUES ('Joseph Bryan', 'Gonzalez Ruiz', 'Midfielder', 'undefined', 'code.jpeg', 'code')"}

 

Sorry for the delayed reply, I think you should also check the sql statement that creates the database and also change the "number" to "numbero"

 

I created an account just to tell you how phenomenal this article is. Clear instruction, great file tree structure. Thank you!

 

Hi, thanks for a great tutorial!
Maybe you can help me? I need to know how to it with PDF files (blob).
Do you have example for that?

 

Thanks, sorry for the delay in replying.
Sadly, I don't have any example concerning uploading pdf files.

 

Hi pale! thanks for sharing,
I want to ask you how to adding some notifications into index.ejs
after we added, updated and deleted players?
I have modified the index.ejs with variable message and
sent the message parameters in every events in player.js

thank you

 

Hey, great tutorial. Been looking all over the Internet for something just like this to introduce me to Node, Express and basic html in a simple way. Thanks.

 
 

This was awesome to read! this article will help me with building my first node mysql app! thanks!

 
 
 

No, I totally forgot about it, I will look into solving it and make updates on the repo. Thanks for pointing it out.

 

Hi ,
A quick one ,
How do I access the phpmyadmin dashboard ?
Thanks

 

I started to learn nodejs today and this is the best tutorial in the world. I feel like I know more and I feel Im a nodejs pro now. Please upload more tutorials on MEAN stack also. Thanks a lot.

 

Thanks for the kind words. I am not very familiar with Angular JS but I can recommend taking a look at this tutorial on YouTube. It covers a lot about building an app using MEAN stack.

 

One of the best and in detail article. Thanks from India.

 
 

Hello, this is an awesome point to start for newbies like me. Thanks a lot for posting your work. Can you elaborate on how to reduce the SQL injection risk?

Cheers

 

Hello sir.
I am facing an error in Index.ejs file. I have attached the snapshot of the error. Please help me out here. It will be a great help.

 

Hello, could you please you send a snapshot of the error?

 

Hi Could you please explain how to build this and copy to cpanel for a domain.? Thanks in advance

 

Hi,
I attached the snapshot of my error can someone help me?

 

Hello, did you make any change to the database? I went through similar errors when I cloned the repo and run the server without creating the original table. I built the players table and then went thru the app analyzing the code and finally made my own app.

 

Hai,

Thank you very much for your POST, I have learned more about the node js and ejs, I am facing problem in view the image. In browser not visible image. If click the view image error cannot get /public/assests/img.png.

Kindly give me the solution for the same.

Please do the needful...

Thanks and regards,
Hari

code of conduct - report abuse