簡體   English   中英

如何使用node.js以及ajax和mysql處理異步編程中的方法?

[英]How can I handle methods in async programming using node.js and ajax and mysql?

我的問題是有關使用Node.JS,ajax和mysql和以下代碼的處理方法。

如果客戶端輸入正確的密碼和用戶名,則服務器向我的前端(index.html)返回“ true”值,如果用戶名和密碼正確返回“ false”,則頁面重定向到chat.html頁面,並顯示一條消息。

但是,當我嘗試使用相同的代碼和用戶信息時,有時會出現“錯誤:寫完之后”,有時卻沒有,並且出現chat.html。 我猜想它與異步編程有關,因為我經常使用“ response.write”,似乎它們與fs.readFile()方法中的代碼混合在一起。

我應該如何替換我的代碼,或者如何克服這種情況? 由於我是node.js的新手,所以對我來說很困惑。

 var http = require ('http'); var url = require('url'); var fs = require('fs'); var json; var mysql = require('mysql'); var con = mysql.createConnection({ host: "127.0.0.1", user: "root", password: "123456", database: "web-chat", timeout:"100ms" }); con.connect(function (err) { if (err){ throw err; } http.createServer(function (request,response) { var q = url.parse(request.url, true); var filename = "."+q.pathname; var uid = q.query.uid; var pass = q.query.pass; console.log(filename); if (q.pathname == "/" || q.pathname == "" || q.pathname=="./") { filename = "./index.html"; } if (q.pathname == "/login"){ console.log("id: "+uid," pass:"+pass); con.query("SELECT * FROM user_table WHERE user_id='"+uid+"' AND password='"+pass+"'", function (err, result, fields) { if (err) throw err; //console.log(result[0].user_id); console.log(uid); if(result==""){ //LOGIN FAILED response.write("false"); return response.end(); } else { //LOGIN SUCCESSFUL response.write("true"); return response.end(); } }); } fs.readFile(filename, function (err, data) { if(err){ console.log("false"); response.writeHead(404,{'Content-Type':'text/html'}); return response.end(); }else{ console.log("true"); response.writeHead(200,{'Content-Type':'text/html'}); response.write(data); return response.end(); } }); }).listen(1337); }); 
 <!DOCTYPE html> <html lang="en"> <head> <style> #frame-div{ border: lightgray solid 1px; border-radius: 3px; width: 40%; margin: auto; } body{ font-family: Calibri; } .label{ display: table-cell; padding-top: 15px; padding-bottom: 5px; } .table{ display: table; } .table-row{ display: table-row; } </style> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div id="frame-div" class="table"> <div class="table-row"> <div class="label">Kullanıcı Adı:</div> <div class="label"><input id="user-id" type="text"></div> </div> <div class="table-row"> <div class="label">Parola:</div> <div class="label"><input id="password" type="password"></div> </div> <div class="table-row"> <div class="label"> <input type="button" value="Giriş Yap" onclick="Login()"> <form action="registration.html" style="display: inline-block"> <input type="submit" value="Üye Ol"> </form></div> </div> </div> <p id="content"></p> <script> function Login() { var userId=document.getElementById("user-id").value; var password=document.getElementById("password").value; var xhttp; xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function () { if(this.readyState == 4 && this.status == 200){ var isregisteredUser=this.responseText; console.log("reg user:"+isregisteredUser); //console.log(this.responseText.uid); if(isregisteredUser=='true'){ window.location.href='chat.html?uid='+userId; }else { document.getElementById("content").innerHTML += "<br/>"+"Hatalı Kullanıcı Adı/Parola"; } } }; xhttp.open("GET","login?uid="+userId+"&pass="+password,true); xhttp.send(); } </script> </body> </html> 

我有時收到的錯誤代碼有時不使用相同的代碼:

events.js:183
  throw er; // Unhandled 'error' event
  ^

Error: write after end
at write_ (_http_outgoing.js:622:15)
at ServerResponse.write (_http_outgoing.js:617:10)
at Query.<anonymous> (C:\Node JS Projects\Web Chat App\server.js:53:30)
at Query.<anonymous> (C:\Node JS Projects\Web Chat App\node_modules\mysql\lib\Connection.js:502:10)
at Query._callback (C:\Node JS Projects\Web Chat App\node_modules\mysql\lib\Connection.js:468:16)
at Query.Sequence.end (C:\Node JS Projects\Web Chat App\node_modules\mysql\lib\protocol\sequences\Sequence.js:83:24)
at Query._handleFinalResultPacket (C:\Node JS Projects\Web Chat App\node_modules\mysql\lib\protocol\sequences\Query.js:139:8)
at Query.EofPacket (C:\Node JS Projects\Web Chat App\node_modules\mysql\lib\protocol\sequences\Query.js:123:8)
at Protocol._parsePacket (C:\Node JS Projects\Web Chat App\node_modules\mysql\lib\protocol\Protocol.js:278:23)
at Parser.write (C:\Node JS Projects\Web Chat App\node_modules\mysql\lib\protocol\Parser.js:76:12)

if (q.pathname == "/login"){然后進入分支,當您從MySQL獲得響應時,編寫一些內容return response.end();

對於任何路徑名,甚至上面的路徑名,都調用fs.readFile ,然后在獲取數據時寫入一些內容並return response.end(); fs.readFile

無論如何,您都需要if/else而不是if

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM