![](/img/trans.png)
[英]GET variable name contains dash creates problems for req.query for NodeJS Express?
[英]Problems with query and express in nodeJS
我正在為帶有nodeJS的服務器使用express開發一個web頁面。 我在注冊頁面上,我正在嘗試驗證用戶插入的數據,但是當我進行查詢時出現錯誤。
auth.js
const express = require('express');
const router = express.Router();
const { bd } = require('../database');
const help_functions = require('../lib/common');
router.post('/signup', async (req,res) => {
const fullname = req.body['fullname'];
const email = req.body['email'];
const username = req.body['username'];
const password = req.body['password'];
const password_repeat = req.body['password_repeat'];
var validation_msg = help_functions.validateSignUp(fullname, email, username, password, password_repeat);
validation_msg = await help_functions.checkRepeatedUserEmail(email);
});
數據庫.js
const mysql = require('mysql');
const { promisify } = require('util');
const database = { // Database credentials }
const bd = mysql.createPool(database);
bd.getConnection((err,connection) => {
if (err) {
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
console.error('Database connection failed !');
}
if (err.code === 'ER_CON_COUNT_ERROR') {
console.error('Database has too many connections !');
}
if (err.code === 'ECONNREFUSED') {
console.error('Database connection was refused !');
}
}
if (connection) {
connection.release();
console.log('Database is connected !');
return;
}
});
bd.query = promisify(bd.query);
module.exports = bd;
common.js
const { bd } = require('../database');
const helper_functions = {}
helper_functions.validateSignUp = (fullname, email, username, password, password_repeat) => {
if (fullname === '' || email === '' || username === '' || password === '' || password_repeat === '') {
return 'All the fields had to be completed!';
}
if (!(password.length >= 8 && (/\d/g.test(password) && (/[A-Z]/.test(password)))) ) {
return 'The password needs to contain at least one capital letter, a number and 8 digits!';
}
if(password != password_repeat) {
return 'Both passwords had to be the same!';
}
return 'Validated!';
}
helper_functions.checkRepeatedUserEmail = async (email) => {
const user = await bd.query('SELECT * FROM users WHERE email = ?', [email]);
if (user.length) {
return 'This email is used, please change it!';
} else {
return 'Validated!';
}
}
module.exports = helper_functions;
錯誤顯示下一個文本:
(node:14616) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'query' of undefined at Object.helper_functions.checkRepeatedUserEmail (proyect_path/src/lib/common.js:19:27)............ ……
(節點:14616) UnhandledPromiseRejectionWarning:未處理的 promise 拒絕。 此錯誤源於在沒有 catch 塊的情況下拋出異步 function 內部,或拒絕未使用.catch() 處理的 promise。 要終止未處理的 promise 拒絕的節點進程,請使用 CLI 標志
--unhandled-rejections=strict
(請參閱https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode )。 (拒絕 ID:2)(節點:14616)[DEP0018] DeprecationWarning:不推薦使用未處理的 promise 拒絕。 將來,未處理的 promise 拒絕將使用非零退出代碼終止 Node.js 進程。
有誰知道發生了什么?? 謝謝閱讀!
您將數據庫公開為database.js
中的默認導出:
module.exports = bd;
但是您正在導入它,就好像它是以db
名稱導出的一樣:
const { bd } = require('../database');
將database.js
中的導出更改為:
module.exports = {
bd: bd
};
或者將其導入common.js
文件中:
const bd = require('../database');
錯誤說 db 沒有在 common.js 文件中定義,可能是你做錯了 require('../database'); 該 require 語句中有錯誤。 使用調試器在該點停止,看看你是否在那里得到 db。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.