我正在开发一个测试NodeJS的应用程序。 登录和注册系统。 所以我测试注册的用户数据是否已经在数据库中。
但它不适用于电子邮件。 我有这个错误:
ReferenceError: mailDispo is not defined
at /var/www/app.js:72:12
at Layer.handle [as handle_request] (/var/www/node_modules/express/lib/router/layer.js:82:5)
at next (/var/www/node_modules/express/lib/router/route.js:110:13)
at Route.dispatch (/var/www/node_modules/express/lib/router/route.js:91:3)
at Layer.handle [as handle_request] (/var/www/node_modules/express/lib/router/layer.js:82:5)
at /var/www/node_modules/express/lib/router/index.js:267:22
at Function.proto.process_params (/var/www/node_modules/express/lib/router/index.js:321:12)
at next (/var/www/node_modules/express/lib/router/index.js:261:10)
at /var/www/node_modules/body-parser/lib/read.js:107:5
at IncomingMessage.onEnd (/var/www/node_modules/body-parser/node_modules/raw-body/index.js:136:7)
但是如果我重新加载页面,虽然我觉得错误就像电子邮件已经被使用了! 一个想法?
Opps我忘记了代码:
var express = require('express');
var body = require('body-parser');
var render = require('ejs');
var app = express();
var mysql = require('mysql');
var mySqlClient = mysql.createConnection({
host: "localhost",
user: "******",
password: "*******!",
database: "******"
});
app.use(body.json()); // to support JSON-encoded bodies
app.use(body.urlencoded({// to support URL-encoded bodies
extended: true
}));
app.get('/', function (request, response) {
var selectQuery = 'SELECT * FROM user';
var sqlQuery = mySqlClient.query(selectQuery);
sqlQuery.on("result", function (row) {
console.log('pseudo: ' + row.pseudo);
console.log('mail: ' + row.mail);
});
sqlQuery.on("end", function () {
mySqlClient.end();
});
sqlQuery.on("error", function (error) {
console.log(error);
});
response.sendfile('html/index.html');
});
app.get('/signup', function (request, response) {
response.render('signup.ejs', {
titre: 'Inscription sur War.io'
});
});
//Inscription
app.post('/signup', function (request, response) {
var pseudo = request.body.pseudo;
var mail = request.body.mail;
var password = request.body.password;
var checkMail = "select id from user where mail=" + mysql.escape(mail);
sqlQueryMail = mySqlClient.query(checkMail);
sqlQueryMail.on("result", function (row) {
if (row.id) {
mailDispo = 0;
errorMail = "Adresse email deja use";
} else {
mailDispo = 1;
}
});
var checkUser = "select id from user where pseudo=" + mysql.escape(pseudo);
sqlQueryUser = mySqlClient.query(checkUser);
sqlQueryUser.on("result", function (row) {
if (row.id) {
pseudoDispo = 0;
errorPseudo = "Pseudo deja use";
} else {
pseudoDispo = 1;
}
});
if(mailDispo == 1 && pseudoDispo == 1){
mySqlClient.query('INSERT INTO user SET ?', {
pseudo: pseudo,
mail: mail,
pass: password
}, function (err, result) {
if (err){
throw err;
} else {
console.log(result.insertId);
}
});
} else {
reponse.render('signup.ejs', {
'titre': 'War.io signup',
'errorPseudo': errorPseudo,
'errorMail': errorMail
});
}
});
app.listen(8080);
答案 0 :(得分:2)
如果我理解正确,您需要在函数的顶层定义变量。这将它设置为函数内部的范围变量,而不是它作为全局对象的属性,这是定义它的内容,如果循环(没有var)将执行。
app.post('/signup', function (request, response) {
var mailDispo;
var pseudo = request.body.pseudo;
var mail = request.body.mail;
var password = request.body.password;
var checkMail = "select id from user where mail=" + mysql.escape(mail);
sqlQueryMail = mySqlClient.query(checkMail);
sqlQueryMail.on("result", function (row) {
if (row.id) {
mailDispo = 0;
errorMail = "Adresse email deja use";
} else {
mailDispo = 1;
}
});