I'm debugging my code and for some reason, the code execution stops after assigning promise.all to a variable, it doesn't execute the lines following. I am also getting errors such as
(node:5) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 17): undefined
though I am not sure if it is related.
It prints the first console.log, but not the last one or anything in between.
here is the problematic section.
console.log('this prints');
result = promise.all([dbinfo, vaultinfo])
.then(array =>{
console.log('recieved all info');
[dbinfo, vaultinfo] = array;
mark(id);
info = {
id: id,
username: dbinfo.username,
country: dbinfo.country,
firstname: vaultinfo.firstname,
lastname: vaultinfo.lastname,
email: vaultinfo.email
};
console.log('logging final info :' , info);
return ( info );
})
.catch( (e) => {
console.log('somethingwent wrong');
console.error(e.message);
return Promise.reject('Error');
});
console.log('this doesnt');
console.log('final results', {result});
return result;
Here is the full code.
let central = require('./central'),
db1 = require('./db1'),
db2 = require('./db2'),
db3 = require('./db3'),
vault = require('./vault'),
mark = require('./mark');
module.exports = function(id) {
// TODO
// Reminder: The deadline is tomorrow !
// to find db, username, and country
var dbinfo = central(id);
dbinfo
.then(data => {
console.log({data});
return data ;
})
.catch( ()=> {
console.log('dbinfo resolution problem:');
return Promise.reject('Error central');
})
.then(dbname=> {
console.log('checking for db');
switch (dbname){
case 'db1': var db = db1(id); break;
case 'db2': var db = db2(id); break;
case 'db3': var db = db3(id); break;
default:
console.log('error db not in file');
Error('Database not in file');
}
db // is a promise, if rejected return name
.catch(()=>{
console.log(dbname +' database returned an error');
throw 'Error '+dbname;
})
console.log('db found, at ' + dbname, db);
return db ;
})
vaultinfo = vault(id);
vaultinfo
.catch( () => {return Promise.reject('Error vault')});
console.log('this prints');
result = promise.all([dbinfo, vaultinfo])
.then(array =>{
console.log('recieved all info');
[dbinfo, vaultinfo] = array;
mark(id);
info = {
id: id,
username: dbinfo.username,
country: dbinfo.country,
firstname: vaultinfo.firstname,
lastname: vaultinfo.lastname,
email: vaultinfo.email
};
console.log('logging final info :' , info);
return ( info );
})
.catch( (e) => {
console.log('somethingwent wrong');
console.error(e.message);
return Promise.reject('Error');
});
console.log('this doesnt');
console.log('final results', {result});
return result;
};