简体   繁体   English

nodeJS中查询和表达的问题

[英]Problems with query and express in nodeJS

I'm developing a web page using express for the server with nodeJS.我正在为带有nodeJS的服务器使用express开发一个web页面。 I'm with the sign up page, and I'm trying to validate user inserted data, but when I make the query I get an error.我在注册页面上,我正在尝试验证用户插入的数据,但是当我进行查询时出现错误。

auth.js 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数据库.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 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;

The error says the next text:错误显示下一个文本:

(node:14616) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'query' of undefined at Object.helper_functions.checkRepeatedUserEmail (proyect_path/src/lib/common.js:19:27).................. (node:14616) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'query' of undefined at Object.helper_functions.checkRepeatedUserEmail (proyect_path/src/lib/common.js:19:27)............ ……

(node:14616) UnhandledPromiseRejectionWarning: Unhandled promise rejection. (节点:14616) UnhandledPromiseRejectionWarning:未处理的 promise 拒绝。 This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with.catch().此错误源于在没有 catch 块的情况下抛出异步 function 内部,或拒绝未使用.catch() 处理的 promise。 To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode ).要终止未处理的 promise 拒绝的节点进程,请使用 CLI 标志--unhandled-rejections=strict (请参阅https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode )。 (rejection id: 2) (node:14616) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. (拒绝 ID:2)(节点:14616)[DEP0018] DeprecationWarning:不推荐使用未处理的 promise 拒绝。 In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.将来,未处理的 promise 拒绝将使用非零退出代码终止 Node.js 进程。

Anyone knows what's happening??有谁知道发生了什么?? Thanks for reading!谢谢阅读!

You are exposing the database as the default export in database.js :您将数据库公开为database.js中的默认导出:

module.exports = bd;

But you are importing it as if it was exported with the name db :但是您正在导入它,就好像它是以db名称导出的一样:

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

Either change the export in database.js to:database.js中的导出更改为:

module.exports = {
    bd: bd
};

Or import it in the common.js file as:或者将其导入common.js文件中:

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

The error says db is not defined in common.js file may be you have done a wrong require('../database');错误说 db 没有在 common.js 文件中定义,可能是你做错了 require('../database'); there is an error in that require statement.该 require 语句中有错误。 Use an debugger to stop at that point and see whether you are getting db at there or not.使用调试器在该点停止,看看你是否在那里得到 db。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM