Reputation: 539
I am trying to make a new data entry in BD with mysql from a page. But when throw me an error about promises
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;
};
When 'conection.query(data.sqlquery()...' is execute the console throw me this error
(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.
So, i cant see where is the error if conection.query(
is declared in the beginning with conection=mysql.createConnection
I am new programming with NodeJs so i i accept any comment or suggestion of good programming practices
valida return a promise declared in another file
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);
}
});
})
};
... for example
Upvotes: 0
Views: 3712
Reputation: 7901
Adding to the answers .
You are using node.js promises . So to handle all type of 'Unhandled promise rejection'. use this code at the main node.js app file
process.on('unhandledRejection', error => {
// Will print "unhandledRejection err is not defined"
console.log('unhandledRejection', error);
});
In case you missed catching some promise this will handle all of them.
Process
Node.js process
is global , it contains unhandledRejection event for handling unhandled promise rejection
.
Upvotes: 0
Reputation: 2191
The error message is a bit misleading here. The important part is TypeError: Cannot read property 'query' of undefined
which is referring to conection
being undefined
when it is called in registrar
when that is called in the promise handler attached to valida.ced
.
The issue is with the code:
var conection = mysql.createConnection({
...
}).connect(function(error) {
...
});
which is assigning the return value of the call to Connection.connect
to your conection
variable. Connection.connection
does not return a value (source), so later when that promise resolves and tries to execute registrar()
, conection
is still and forever undefined, so there's no such thing as conection.query
to be called. Try:
var conection = mysql.createConnection({
...
});
conection.connect(function(error) {
...
});
As T.J. points out, the other important part of the error message is that you should provide code to handle rejected promises. Use the catch
function to attach these handlers.
Upvotes: 1
Reputation: 1074365
Will's explained why your code is failing. The reason you're seeing it as an "Unhandled rejection" error is that you're not handling your promises correctly.
One of the rules of promises is that you either pass on the promise chain to the level above you, or you handle errors. This code does neither (nor do others written largely the same way):
valida.user(data).then((rows) => {
validaciones.user = rows;
})
What if the promise returned by valida.user(data)
rejects? Answer: Nothing handles it.
You must either hand off the promise then
creates to something else, or handle rejections. (If you hand it off, the thing you had it off to has to hand it off again, or handle errors.)
To make that code handle errors, add a catch
handler:
valida.user(data).then((rows) => {
validaciones.user = rows;
}).catch(err => {
// Do something with the error here
});
Upvotes: 1