简体   繁体   English

使用 express.js 和 node.js(服务器端)和 react.js(客户端)发布查询问题

[英]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.我是一名初级开发人员,在前端使用 ReactJS 时,我遇到了在 node.js、express.js 中进行的后查询的问题。

I have two folders: client and server我有两个文件夹:客户端和服务器

In the server folder I am using node.js and express.js.在服务器文件夹中,我使用的是 node.js 和 express.js。 Here is the code in the index.js file from the server side:这是服务器端 index.js 文件中的代码:

在此处输入图像描述

在此处输入图像描述

In the client folder I am using node.js and express.js.在客户端文件夹中,我使用的是 node.js 和 express.js。 Here is the code in the App.js file from the client side:这是来自客户端的 App.js 文件中的代码:

在此处输入图像描述

My server is running on the port 3306 and the code is ok, client side as server side.我的服务器在端口 3306 上运行,代码没问题,客户端作为服务器端。

The issue I am having is with the db.我遇到的问题是 db. I created a db in sequel pro « employeeSystem » with a table « employee ».我在sequel pro «employeeSystem»中创建了一个带有表«employee»的数据库。

And I cannot insert the values in the table.而且我无法在表中插入值。

Error message 1:错误信息 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)在 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.错误消息 2:{ 致命:true,代码:'PROTOCOL_CONNECTION_LOST'} 错误:致命错误后无法将查询入队。

I am a bit lost on the MySQL side and with the db I created in Sequel Pro.我在 MySQL 方面和我在 Sequel Pro 中创建的数据库有点迷失了。

在此处输入图像描述

In the browser here is the localhost:3000在浏览器中,这里是 localhost:3000

在此处输入图像描述

and the localhost:3306 working well和 localhost:3306 运行良好

在此处输入图像描述

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.问题是您使用的是mysql.createConnection() ,它旨在在查询完成后立即打开和关闭,而不是在应用程序的生命周期内保持打开状态。

For long lasting connections, you will want to use mysql.createPool() which is designed for connections lasting as long as the app does.对于持久的连接,您将需要使用mysql.createPool() ,它专为与应用程序一样持久的连接而设计。 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() createPool() - arguments 与createConnection()相同
connection.end() - takes in a function connection.end() - 接收 function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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