【问题标题】:Problems with query and express in nodeJSnodeJS中查询和表达的问题
【发布时间】:2020-08-09 03:34:52
【问题描述】:

我正在使用 express 为带有 nodeJS 的服务器开发一个网页。我在注册页面,我正在尝试验证用户插入的数据,但是当我进行查询时出现错误。

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);
});

database.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: 无法读取 未定义的属性“查询” 在 Object.helper_functions.checkRepeatedUserEmail (proyect_path/src/lib/common.js:19:27) ....................

(node:14616) UnhandledPromiseRejectionWarning: 未处理的承诺 拒绝。此错误源于在异步内部抛出 没有 catch 块的函数,或者通过拒绝一个承诺 不使用 .catch() 处理。终止未处理的节点进程 承诺拒绝,使用 CLI 标志 --unhandled-rejections=strict (见https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode)。 (拒绝 ID:2)(节点:14616)[DEP0018] DeprecationWarning:未处理 不推荐使用承诺拒绝。在未来,承诺拒绝 未处理的将终止 Node.js 进程 非零退出代码。

谁知道发生了什么?? 感谢阅读!

【问题讨论】:

    标签: javascript mysql node.js node-promisify


    【解决方案1】:

    您将数据库公开为database.js 中的默认导出:

    module.exports = bd;
    

    但是您正在导入它,就好像它是以名称 db 导出的一样:

    const { bd } = require('../database');
    

    要么将database.js 中的导出更改为:

    module.exports = {
        bd: bd
    };
    

    或者在common.js文件中导入为:

    const bd = require('../database');
    

    【讨论】:

    • 这可以重写为 - module.exports.bd = bd;
    【解决方案2】:

    错误说 db is not defined in common.js 文件可能是你做错了 require('../database');该 require 语句中有错误。 使用调试器在该点停止,看看你是否在那里得到 db。

    【讨论】:

    • 我认为问题出在查询中 >>> await bd.query('SELECT * FROM users WHERE email = ?', [email]);。数据库连接完成,但是执行此行时出现错误。
    猜你喜欢
    • 1970-01-01
    • 2012-03-20
    • 1970-01-01
    • 2019-02-08
    • 2012-05-01
    • 2016-09-10
    • 1970-01-01
    • 2019-05-27
    • 1970-01-01
    相关资源
    最近更新 更多