{"id":13353,"date":"2023-12-12T00:01:26","date_gmt":"2023-12-11T16:01:26","guid":{"rendered":"https:\/\/92it.top\/?p=13353"},"modified":"2023-12-12T00:01:26","modified_gmt":"2023-12-11T16:01:26","slug":"nodejs%e4%bd%bf%e7%94%a8async-await%e5%90%8c%e6%ad%a5%e6%93%8d%e4%bd%9cmysql","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=13353","title":{"rendered":"nodejs\u4f7f\u7528async\/await\u540c\u6b65\u64cd\u4f5cmysql"},"content":{"rendered":"\n<p>Node.js\u7684\u6838\u5fc3\u6982\u5ff5\u662f\u975e\u963b\u585eIO\u548c\u5f02\u6b65\u7f16\u7a0b\u3002\u867d\u7136\u8fd9\u79cd\u673a\u5236\u7ed9Node.js\u5e26\u6765\u4e86\u5de8\u5927\u7684\u4f18\u52bf\u548c\u597d\u5904\uff0c\u4f46\u540c\u65f6\u5b83\u4e5f\u5e26\u6765\u4e86\u8bb8\u591a\u95ee\u9898\u548c\u6311\u6218\uff0c\u6bd4\u5982\u6211\u4eec\u5728\u505a\u4e00\u4e9b\u5f02\u6b65\u64cd\u4f5c\u7684\u65f6\u5019\uff0c\u5982\u679c\u9700\u8981\u62ff\u5230\u5f02\u6b65\u64cd\u4f5c\u8fd4\u56de\u7684\u7ed3\u679c\u4e4b\u540e\u518d\u8fdb\u884c\u4e0b\u4e00\u6b65\u64cd\u4f5c\uff0c\u901a\u5e38\u9700\u8981\u901a\u8fc7\u4e00\u5c42\u5c42\u7684\u5f02\u6b65\u56de\u8c03\uff0c\u5bfc\u81f4\u7ef4\u62a4\u5341\u5206\u56f0\u96be\uff0c\u4ee3\u7801\u53ef\u8bfb\u6027\u5dee\uff0ckoa2\u5929\u751f\u5c31\u652f\u6301async\u7279\u6027\uff0c\u5f53\u7136\u76ee\u524d\u5728express\u4e5f\u53ef\u4ee5\u4f7f\u7528\u3002<\/p>\n\n\n\n<p><strong>\u5b89\u88c5 mysql\u9a71\u52a8<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u7136\u5728\u6b64\u4e4b\u524d\u4f60\u5fc5\u987b\u5148\u5b89\u88c5\u5e76\u542f\u52a8mysql<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">npm install mysql --save<\/pre>\n\n\n\n<p><strong>\u521b\u5efamysql\u8fde\u63a5<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">const mysql      = require('mysql')\n\nconst connection = mysql.createConnection({\n  host     : '127.0.0.1',   \/\/ \u6570\u636e\u5e93\u5730\u5740\n  user     : 'root',    \/\/ \u6570\u636e\u5e93\u7528\u6237\n  password : '123456'   \/\/ \u6570\u636e\u5e93\u5bc6\u7801\n  database : 'my_database'  \/\/ \u9009\u4e2d\u6570\u636e\u5e93\n})\n\n\/\/ \u8fde\u63a5\u6570\u636e\u5e93 \u8fd9\u4e00\u6b65\u4e0d\u662f\u5fc5\u987b\u7684 \u56e0\u4e3a\u5728query\u7684\u65f6\u5019\u4f1a\u9ed8\u8ba4\u8fde\u63a5\nconnection.connect((err) => {\n    \/\/ \u5982\u679c\u5728\u8fd9\u4e00\u6b65\u629b\u51fa\u9519\u8bef \u8bf7\u68c0\u67e5\u6570\u636e\u5e93\u914d\u7f6e  \u6bd4\u5982\u6743\u9650 \u9009\u4e2d\u6570\u636e\u5e93\u662f\u5426\u5b58\u5728\u7b49\u7b49..\n    if (err) return console.log('\u6570\u636e\u5e93\u8fde\u63a5\u5931\u8d25', err.message);\n    console.log('\u6570\u636e\u5e93\u8fde\u63a5\u6210\u529f');\n})\n\n\/\/ \u6267\u884csql\u811a\u672c\u5bf9\u6570\u636e\u5e93\u8fdb\u884c\u8bfb\u5199 \nconnection.query('SELECT * FROM my_table',  (error, results, fields) => {\n  if (error) throw error\n  \/\/ connected! \n\n  \/\/ \u7ed3\u675f\u4f1a\u8bdd\n  connection.release() \n});\n<\/pre>\n\n\n\n<p>\u6ce8\u610f\uff1a\u4e00\u4e2a\u4e8b\u4ef6\u5c31\u6709\u4e00\u4e2a\u4ece\u5f00\u59cb\u5230\u7ed3\u675f\u7684\u8fc7\u7a0b\uff0c\u6570\u636e\u5e93\u4f1a\u8bdd\u64cd\u4f5c\u6267\u884c\u5b8c\u540e\uff0c\u5c31\u9700\u8981\u5173\u95ed\u6389\uff0c\u4ee5\u514d\u5360\u7528\u8fde\u63a5\u8d44\u6e90\u3002<\/p>\n\n\n\n<p><strong>\u521b\u5efa\u6570\u636e\u8fde\u63a5\u6c60<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4e0d\u4f1a\u6309\u7167\u4e0a\u9762\u7684\u505a\u6cd5\uff0c\u56e0\u4e3a\u4e00\u822c\u64cd\u4f5c\u6570\u636e\u5e93\u662f\u5f88\u590d\u6742\u7684\u8bfb\u5199\u8fc7\u7a0b\uff0c\u4e0d\u53ea\u662f\u4e00\u4e2a\u4f1a\u8bdd\uff0c\u5982\u679c\u76f4\u63a5\u7528\u4f1a\u8bdd\u64cd\u4f5c\uff0c\u5c31\u9700\u8981\u6bcf\u6b21\u4f1a\u8bdd\u90fd\u8981\u914d\u7f6e\u8fde\u63a5\u53c2\u6570\uff0c\u6240\u4ee5\u8fd9\u65f6\u5019\u5c31\u9700\u8981\u8fde\u63a5\u6c60\u7ba1\u7406\u4f1a\u8bdd(\u5982\u679c\u4f7f\u7528MongoDB\u5219\u65e0\u9700\u62c5\u5fc3\u8fd9\u4e9b\u95ee\u9898mongo\u4f1a\u7ba1\u7406\u81ea\u5df1\u7684\u8fde\u63a5\u96c6\u5408\uff0c\u6216\u8fde\u63a5&#8221;\u6c60&#8221;)<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">const mysql = require('mysql')\n\n\/\/ \u521b\u5efa\u6570\u636e\u6c60\nconst pool  = mysql.createPool({\n  host     : '127.0.0.1',   \/\/ \u6570\u636e\u5e93\u5730\u5740\n  user     : 'root',    \/\/ \u6570\u636e\u5e93\u7528\u6237\n  password : '123456'   \/\/ \u6570\u636e\u5e93\u5bc6\u7801\n  database : 'my_database'  \/\/ \u9009\u4e2d\u6570\u636e\u5e93\n})\n\n\/\/ \u5728\u6570\u636e\u6c60\u4e2d\u8fdb\u884c\u4f1a\u8bdd\u64cd\u4f5c\npool.getConnection(function(err, connection) {\n\n  connection.query('SELECT * FROM my_table',  (error, results, fields) => {\n\n    \/\/ \u7ed3\u675f\u4f1a\u8bdd\n    connection.release();\n\n    \/\/ \u5982\u679c\u6709\u9519\u8bef\u5c31\u629b\u51fa\n    if (error) throw error;\n  })\n})\n<\/pre>\n\n\n\n<p><strong>\u5c01\u88c5mysql\u8bf7\u6c42<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u524d\u9762\u5185\u5bb9\u4f5c\u4e3a\u524d\u7f6e\u77e5\u8bc6\uff0c\u7531\u4e8emysql\u6a21\u5757\u7684\u64cd\u4f5c\u90fd\u662f\u5f02\u6b65\u64cd\u4f5c\uff0c\u6bcf\u6b21\u64cd\u4f5c\u7684\u7ed3\u679c\u90fd\u662f\u5728\u56de\u8c03\u51fd\u6570\u4e2d\u6267\u884c\uff0c\u73b0\u5728\u6709\u4e86async\/await\uff0c\u5c31\u53ef\u4ee5\u7528\u540c\u6b65\u7684\u5199\u6cd5\u53bb\u64cd\u4f5c\u6570\u636e\u5e93<br>\u5bf9\u4e8easync\/await\u4e0d\u719f\u6089\u7684\u670b\u53cb\u8bf7\u5148\u67e5\u9605\u76f8\u5173\u6587\u6863\u4ee5\u4e86\u89e3\u7528\u6cd5\uff0c\u4e0d\u8fc7\u6ca1\u719f\u6089\u4e5f\u6ca1\u5173\u7cfb\uff0c\u8bb0\u4f4fasync\/await \u9700\u8981\u8fd4\u56de\u4e00\u4e2a Promise \u5bf9\u8c61\u5c31\u53ef\u4ee5\u4e86.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">const mysql = require('mysql')\nconst pool = mysql.createPool({\n  host     :  '127.0.0.1',\n  user     :  'root',\n  password :  '123456',\n  database :  'my_database'\n})\n\n\/\/ \u63a5\u6536\u4e00\u4e2asql\u8bed\u53e5 \u4ee5\u53ca\u6240\u9700\u7684values\n\/\/ \u8fd9\u91cc\u63a5\u6536\u7b2c\u4e8c\u53c2\u6570values\u7684\u539f\u56e0\u662f\u53ef\u4ee5\u4f7f\u7528mysql\u7684\u5360\u4f4d\u7b26 '?'\n\/\/ \u6bd4\u5982 query(`select * from my_database where id = ?`, [1])\n\nlet query = function( sql, values ) {\n  \/\/ \u8fd4\u56de\u4e00\u4e2a Promise\n  return new Promise(( resolve, reject ) => {\n    pool.getConnection(function(err, connection) {\n      if (err) {\n        reject( err )\n      } else {\n        connection.query(sql, values, ( err, rows) => {\n\n          if ( err ) {\n            reject( err )\n          } else {\n            resolve( rows )\n          }\n          \/\/ \u7ed3\u675f\u4f1a\u8bdd\n          connection.release()\n        })\n      }\n    })\n  })\n}\n\nmodule.exports =  query<\/pre>\n\n\n\n<p>\u7b80\u5355\u7684\u6211\u4eec\u5c31\u5c01\u88c5\u597d\u4e86,\u8fd9\u4e2a\u65f6\u5019\u6211\u4eec\u53ef\u4ee5\u8fd9\u4e48\u4f7f\u7528<\/p>\n\n\n\n<p>express<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">router.post('\/login', async (req, res) => {\n    \n    \/\/ \u539f\u6765\u505a\u6cd5\n    \/\/ query('select * from im_user', (err, rows) => {\n    \/\/     res.json({\n    \/\/         code: 0,\n    \/\/         msg: '\u8bf7\u6c42\u6210\u529f',\n    \/\/         data: rows\n    \/\/     })\n    \/\/ })\n    \n    \/\/ \u73b0\u5728\n    const rows = await query('select * from im_user')\n    res.json({\n        code: 0,\n        msg: '\u8bf7\u6c42\u6210\u529f',\n        data: rows\n    })\n    \n})<\/pre>\n\n\n\n<p>\u5f53\u7136 \u5982\u679c\u4f60\u89c9\u5f97\u628asql\u8bed\u53e5\u6563\u843d\u5728\u5404\u4e2a\u5730\u65b9\u4e0d\u592a\u597d\uff0c\u4f60\u53ef\u4ee5\u7edf\u4e00\u7ba1\u7406.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Node.js\u7684\u6838\u5fc3\u6982\u5ff5\u662f\u975e\u963b\u585eIO\u548c\u5f02\u6b65\u7f16\u7a0b\u3002\u867d\u7136\u8fd9\u79cd\u673a\u5236\u7ed9Node.js\u5e26\u6765\u4e86\u5de8\u5927\u7684\u4f18\u52bf\u548c\u597d\u5904\uff0c\u4f46\u540c\u65f6\u5b83\u4e5f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,23],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/13353"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=13353"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/13353\/revisions"}],"predecessor-version":[{"id":13354,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/13353\/revisions\/13354"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=13353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=13353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=13353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}