[英]Error “Unhandled promise rejection” with mysql query
我正在尝试从MySQL在BD中使用mysql创建新的数据条目。 但是当丢给我关于承诺的错误时
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
我是使用NodeJ进行编程的新手,所以ii接受对良好编程习惯的任何评论或建议
有效期返回在另一个文件中声明的承诺
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); } }); }) };
... 例如
错误消息在这里有点误导。 重要的部分是TypeError: Cannot read property 'query' of undefined
这是指conection
在registrar
中调用时在连接到valida.ced
的promise处理程序中调用时undefined
连接。
问题在于代码:
var conection = mysql.createConnection({
...
}).connect(function(error) {
...
});
这conection
对Connection.connect
的调用的返回值分配给您的conection
变量。 Connection.connection
不会返回值( source ),因此稍后当该诺言解析并尝试执行registrar()
, conection
仍然是永远未定义的,因此不存在诸如conection.query
之类的事情。 尝试:
var conection = mysql.createConnection({
...
});
conection.connect(function(error) {
...
});
正如TJ指出的那样,错误消息的另一个重要部分是您应该提供代码来处理被拒绝的承诺。 使用catch
函数来附加这些处理程序。
威尔将解释您的代码为何失败。 您将其视为“未处理的拒绝”错误的原因是,您未正确处理承诺。
承诺的规则之一是您要么将承诺链传递到上一级,要么处理错误。 这段代码既不做(其他人写的也基本相同):
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
});
添加到答案。
您正在使用node.js promises。 因此要处理所有类型的“未处理的承诺拒绝”。 在主要的node.js应用文件中使用此代码
process.on('unhandledRejection', error => {
// Will print "unhandledRejection err is not defined"
console.log('unhandledRejection', error);
});
如果您错过了兑现诺言,这将处理所有这些诺言。
处理
Node.js process
是全局的,它包含unhandledRejection事件,用于处理unhandled promise rejection
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.