我正在尝试从页面用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=mysql.createConnection声明conection.query(,我看不出哪里是错误
我是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);
}
});
})
};
..。例如
发布于 2017-11-19 23:43:15
这里的错误消息有点误导人。重要的部分是TypeError: Cannot read property 'query' of undefined,它指的是conection在registrar中调用时为undefined,在附加到valida.ced的promise处理程序中调用时。
问题出在代码上:
var conection = mysql.createConnection({
...
}).connect(function(error) {
...
});它将调用Connection.connect的返回值赋给您的conection变量。Connection.connection不返回值(source),因此稍后当promise解析并尝试执行registrar()时,conection仍然是未定义的,因此没有conection.query可供调用。尝试:
var conection = mysql.createConnection({
...
});
conection.connect(function(error) {
...
});正如T.J.指出的,错误消息的另一个重要部分是您应该提供代码来处理被拒绝的承诺。使用catch函数附加这些处理程序。
发布于 2017-11-19 23:43:46
Will's explained你的代码失败的原因。你之所以认为它是一个“未经处理的拒绝”错误,是因为你没有正确地处理你的承诺。
promise的规则之一是,您要么将promise链传递到您上面的级别,要么您处理错误。这段代码不会(其他代码也不会以相同的方式编写):
valida.user(data).then((rows) => {
validaciones.user = rows;
})如果valida.user(data)返回的承诺被拒绝了怎么办?答:没有什么能解决这个问题。
您必须将then创建的promise传递给其他对象,或者处理拒绝。(如果你传递它,你拥有它的东西必须再次传递它,或者处理错误。)
要使该代码处理错误,请添加一个catch处理程序:
valida.user(data).then((rows) => {
validaciones.user = rows;
}).catch(err => {
// Do something with the error here
});发布于 2017-11-20 00:39:15
添加到答案中。
您正在使用node.js promises。所以要处理所有类型的“未经处理的拒绝承诺”。在主node.js应用程序文件中使用此代码
process.on('unhandledRejection', error => {
// Will print "unhandledRejection err is not defined"
console.log('unhandledRejection', error);
});如果你错过了一些承诺,这将会解决所有的问题。
过程
Node.js process是全局的,它包含用于处理unhandled promise rejection的unhandledRejection事件。
https://stackoverflow.com/questions/47378531
复制相似问题