Problems with the MySQL module in node

Source: Internet
Author: User

  Node's MySQL module, I feel is not easy to use, all kinds of error, a variety of pits, there is a problem plagued me for a long time, do not know is not the way I used the wrong, but later with the Easymysql module solved, I am convinced that this is a pit.

Problem Description:

Suppose there is a table table with two fields index and name, a button on the localhost:3000 home page that says "Update", and when I click Update Post to Localhost:3000/update, Then the background connection database for the update operation the name of each record is "GDT", after the update is finished, return to the homepage, and then press the "Update" button, will report an error, as follows:

{[Error:cannot enqueue Query after invoking quit.] Code: ' Protocol_enqueue_after_quit ', Fatal:false} "

The following code omits the introduction of the module and the connection operation of the database, I use asynchronous Process Control Async to make the asynchronous query into a synchronous query, at the end of the process ends connection will end, the route will redirect to Localhost:3000, when I re-press " Update button will continue with the update instead of an error

Exports.update = function (req, res) {//Get all record information required function GetInfo () {return new Promise (Resolve, Reject)                    = = {Connection.query (' SELECT ' index ', ' name ' from table ', function (err, result) {if (err) {                Console.log (ERR);                } else {resolve (result); }})}//update the name of each item to GDT function UpdateName (info) {return new Promise (Resolve, Reje            CT) = = {var index = info[' index '];                Connection.query ("UPDATE ' table ' SET ' TableB '. ' Name ' = ' GDT ' WHERE ' table '. ' Index ' =?", index, function (err, result) {                if (err) {Console.log (err);                } else {resolve (' update succeeded '); }})})} Async function Update () {
let result = '; Filter out the global uid that appears in Gang and its corresponding index var getinforesult = await getInfo (); for (Let i = 0; i < getinforesult.length; i++) {var updateresult = await updatename (getinforesult[i]); Console.log (Updateresult); }
Return Result?result: ' Success '; Update (). Then (function (Result) {Console.log (result); Disconnect database connection connection.end (); Back to homepage res.redirect ('/'); })}

This question I consulted a lot of information, asked a lot of people have not been solved, and then switched to the Easymysql module to solve, the asynchronous process control after the end of the page redirect to the homepage, re-press "Update" will perform the update operation, no error!

The above code is not changed to easymysql syntax, the use of easymysql can be found in https://github.com/aleafs/easymysql, but it is recommended that the node project with a NoSQL database like MongoDB , I would not use MySQL for the company's business requirements.

Problems with the MySQL module in node

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.