错误"未处理的承诺拒绝"用mysql查询

时间:2017-11-19 15:26:21

标签: javascript mysql node.js

我正在尝试使用来自页面的mysql在BD中创建一个新的数据条目。但是当我向我提出有关承诺的错误时

var mysql = require('mysql'); //Llamadi a MySql
var valida=         require('./modulos/validaciones');

var conection = mysql.createConnection({
    host: 'localhost', //Host
    user: 'eleazarsb', //Usuario
    password: 'eleazar616', //Contraseña
    database: 'mydb' //Base de datos
}).connect(function(error) {
    if (error) console.log('Problemas de conexion con mysql');
    console.log("conexion Exitosa!");
});

var validaciones = {
    user: false,
    mail: false,
    ced: false
}

//Pendiente: 02
valida.user(data).then((rows) => {
    validaciones.user = rows;
})
valida.mail(data).then((rows) => {
    validaciones.mail = rows;
})
valida.ced(data).then((rows) => {
    validaciones.ced = rows;
    registrar(validaciones);
})


function registrar(validaciones) {
    if (validaciones.user == false && validaciones.mail == false && validaciones.ced == false) {
        var query = conection.query(data.sqlquery(), (error, columna, campos) => {
            if (error) throw error;
            console.log("Entra en registro de BD");
            console.log(columna);
        });
    }
    else {
        console.log("No se registro nada");
    };

    return query;
};

当' conection.query(data.sqlquery()...'执行控制台时抛出此错误 (node:1588) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'query' of undefined (node:1588) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

所以,如果在conection.query(

的开头宣布conection=mysql.createConnection,我就无法看到错误在哪里

我是NodeJs的新编程,所以我接受任何关于良好编程实践的评论或建议

valida返回在另一个文件中声明的承诺



exports.mail= (data)=>{
	return new Promise ((resolve,reject)=>{
		var query=conection.query(data.valida_mail(),(error,rows)=>{
			if (error) return reject  (error);
			if(rows[0]){
				console.log("Email Invalido!");
				resolve(true);
			} else {
				console.log("Email Valido");
				resolve(false);
			}
		});
	})
};




...例如

3 个答案:

答案 0 :(得分:1)

错误消息在这里有点误导。重要的部分是TypeError: Cannot read property 'query' of undefined,当conectionundefined中调用registrar时,在valida.ced附加的声明处理程序中调用var conection = mysql.createConnection({ ... }).connect(function(error) { ... }); 时,Connection.connectconection

问题在于代码:

Connection.connection

registrar()的调用的返回值分配给conection变量。 conection.query不返回值(source),因此稍后当该承诺结算并尝试执行var conection = mysql.createConnection({ ... }); conection.connect(function(error) { ... }); 时,catch仍然是永远未定义的,因此'没有$name被调用的东西。尝试:

numbers.txt

作为T.J.指出,其他错误消息的重要部分是您应该提供代码来处理被拒绝的承诺。使用echo -n "" read name number=$(grep "$name" numbers.txt | awk -F';' '{print $2}') echo "$number" 功能附加这些处理程序。

答案 1 :(得分:1)

Will's explained为什么你的代码失败了。您之所以将其视为"未处理拒绝"错误是您没有正确处理您的承诺。

承诺的规则之一是您将承诺链传递到您之上的级别,或者您处理错误。这段代码既没有(也没有其他人写的大致相同):

valida.user(data).then((rows) => {
    validaciones.user = rows;
})

如果valida.user(data)返回的承诺拒绝怎么办?答:没有办法处理它。

您必须将承诺then传递给其他人,或处理拒绝。 (如果你把它关掉,你把它拿走的东西必须再把它拿掉,或处理错误。)

要使该代码处理错误,请添加catch处理程序:

valida.user(data).then((rows) => {
    validaciones.user = rows;
}).catch(err => {
    // Do something with the error here
});

答案 2 :(得分:0)

添加答案。

您正在使用node.js承诺。所以要处理所有类型的未处理承诺拒绝'。在主node.js应用程序文件中使用此代码

process.on('unhandledRejection', error => {
  // Will print "unhandledRejection err is not defined"
  console.log('unhandledRejection', error);
});

如果你错过了一些承诺,这将处理所有这些。

  

过程

Node.js process是全局的,它包含用于处理unhandled promise rejection的unhandledRejection事件。