ReferenceError: connection is not defined nodejs mysql
ReferenceError: connection is not defined nodejs mysql
我关注这个网站是为了创建一个基本的登录系统:https://codeshack.io/basic-login-system-nodejs-express-mysql/。
我的问题是,当我向用户名和密码按钮输入任何值时,我会在这一行中收到 ReferenceError: connection is not defined:connection.query('SELECT * FROM accounts WHERE username = ? AND password = ?', [用户名, 密码],
我检查数据库连接。 我已连接数据库,数据库与table存在。另外,我安装了 mysql 包。
这是我的源代码:
const mysql = require('mysql');
const express = require('express');
const session = require('express-session');
const bodyParser = require('body-parser');
const path = require('path');
const con = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'password',
database: 'nodelogin'
});
const app = express();
app.use(session({
secret: 'secret',
resave: true,
saveUninitialized: true
}));
app.use(bodyParser.urlencoded({extended : true}));
app.use(bodyParser.json());
app.get('/', (req, res)=> {
res.sendFile(path.join(__dirname + '/login.html'));
});
app.post('/auth', (req, res)=>{
const username = req.body.username;
const password = req.body.password;
const string = "SELECT * FROM accounts WHERE username = ? AND password = ?"
if (username && password) {
connection.query('SELECT * FROM accounts WHERE username = ? AND password = ?', [username, password], (err, results, fields)=> {
if (results.length > 0) {
req.session.loggedin = true;
req.session.username = username;
res.redirect('/home');
} else {
res.send('Incorrect Username and/or Password!');
}
res.end();
});
} else {
res.send('Please enter Username and Password!');
res.end();
}
});
app.get('/home', (req, res)=> {
if (req.session.loggedin) {
res.send('Welcome back, ' + req.session.username + '!');
} else {
res.send('Please login to view this page!');
}
res.end();
});
con.connect(function(err){
if(err) throw err;
console.log("connected");
});
app.listen(8000);
const port = process.env.PORT || 8000;
console.log(`sercer is running on ${port} .`);
您刚刚调用了连接 'con',因此 connection.query 无法正常工作,更改为 con.query
您正在使用名为 con
的变量连接到 SQL
,但使用名为 connection
的变量连接到 运行 查询,即 undefined
。
将您的 connection.query
更新为 con.query
我关注这个网站是为了创建一个基本的登录系统:https://codeshack.io/basic-login-system-nodejs-express-mysql/。
我的问题是,当我向用户名和密码按钮输入任何值时,我会在这一行中收到 ReferenceError: connection is not defined:connection.query('SELECT * FROM accounts WHERE username = ? AND password = ?', [用户名, 密码],
我检查数据库连接。 我已连接数据库,数据库与table存在。另外,我安装了 mysql 包。
这是我的源代码:
const mysql = require('mysql');
const express = require('express');
const session = require('express-session');
const bodyParser = require('body-parser');
const path = require('path');
const con = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'password',
database: 'nodelogin'
});
const app = express();
app.use(session({
secret: 'secret',
resave: true,
saveUninitialized: true
}));
app.use(bodyParser.urlencoded({extended : true}));
app.use(bodyParser.json());
app.get('/', (req, res)=> {
res.sendFile(path.join(__dirname + '/login.html'));
});
app.post('/auth', (req, res)=>{
const username = req.body.username;
const password = req.body.password;
const string = "SELECT * FROM accounts WHERE username = ? AND password = ?"
if (username && password) {
connection.query('SELECT * FROM accounts WHERE username = ? AND password = ?', [username, password], (err, results, fields)=> {
if (results.length > 0) {
req.session.loggedin = true;
req.session.username = username;
res.redirect('/home');
} else {
res.send('Incorrect Username and/or Password!');
}
res.end();
});
} else {
res.send('Please enter Username and Password!');
res.end();
}
});
app.get('/home', (req, res)=> {
if (req.session.loggedin) {
res.send('Welcome back, ' + req.session.username + '!');
} else {
res.send('Please login to view this page!');
}
res.end();
});
con.connect(function(err){
if(err) throw err;
console.log("connected");
});
app.listen(8000);
const port = process.env.PORT || 8000;
console.log(`sercer is running on ${port} .`);
您刚刚调用了连接 'con',因此 connection.query 无法正常工作,更改为 con.query
您正在使用名为 con
的变量连接到 SQL
,但使用名为 connection
的变量连接到 运行 查询,即 undefined
。
将您的 connection.query
更新为 con.query