简体   繁体   中英

post query issue with express.js and node.js (server side) and react.js (client side)

I am a junior developer and I am facing an issue with a post query made in node.js, express.js while using ReactJS on the front-end.

I have two folders: client and server

In the server folder I am using node.js and express.js. Here is the code in the index.js file from the server side:

在此处输入图像描述

在此处输入图像描述

In the client folder I am using node.js and express.js. Here is the code in the App.js file from the client side:

在此处输入图像描述

My server is running on the port 3306 and the code is ok, client side as server side.

The issue I am having is with the db. I created a db in sequel pro « employeeSystem » with a table « employee ».

And I cannot insert the values in the table.

Error message 1:

Error: Connection lost: The server closed the connection. at Protocol.end (/Users/ana/Desktop/crud-tutorial/server/node_modules/mysql/lib/protocol/Protocol.js:112:13)

Error message 2: { fatal: true, code: 'PROTOCOL_CONNECTION_LOST' } Error: Cannot enqueue Query after fatal error.

I am a bit lost on the MySQL side and with the db I created in Sequel Pro.

在此处输入图像描述

In the browser here is the localhost:3000

在此处输入图像描述

and the localhost:3306 working well

在此处输入图像描述

Thank you very much in advance if you can help!

I know this was asked a while ago, but I'd thought I'd answer incase you're still stuck.

The issue is that you're using mysql.createConnection() which is designed to be opened and closed as soon as the query has finished, and not kept open for the lifecycle of your app.

For long lasting connections, you will want to use mysql.createPool() which is designed for connections lasting as long as the app does. Alternatively, you could open the connection when you need to interact with the database, and close the connection after the interaction has finished.

Here are some links:

createPool() - the arguments are the same as createConnection()
connection.end() - takes in a function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM