我正在尝试使用来自页面的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);
}
});
})
};

...例如
答案 0 :(得分:1)
错误消息在这里有点误导。重要的部分是TypeError: Cannot read property 'query' of undefined
,当conection
在undefined
中调用registrar
时,在valida.ced
附加的声明处理程序中调用var conection = mysql.createConnection({
...
}).connect(function(error) {
...
});
时,Connection.connect
为conection
。
问题在于代码:
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事件。