As you can see in the .gif below, the first time I call my database it returns data successfully. But the next two times it neither returns nor errors, the asynchronous code simply disappears. And even worse, my await statement doesn't wait for the response, it just continues.
Can anyone tell me why the subsequent calls to Sequelize disappear? And more importantly, why the calling code keeps running even though I've used await
?
Here is the innermost code:
getRolesAndPermissionsForUser(userId: string): Promise<[IRoleModel]> {
return new Promise<[IRoleModel]>( async (resolve: any, reject: any) => {
try {
const rows = await this._db.getSequelize().transaction( (transaction) => {
return this._db.getSequelize().query(getRolesAndPermissionsForUserSql, { type: Sequelize.QueryTypes.SELECT, replacements: [userId] });
})
resolve(this.convertRolesToTree(rows));
}
catch (error) { reject(error); }
});
}
called by:
async function hasPermission(hapiResolve: (response: any) => void, requestingUserId: string, requestedUserId: string, requestedUsersRoles: any[]): Promise<boolean> {
return new Promise<boolean>( (resolve, reject) => {
try {
//user is seeng her own roles
let result = (requestingUserId === requestedUserId);
//user manages a franchisor the requestee is part of
if (!result) {
requestedUsersRoles.forEach( async (role) => {
if (!result) { //may have found it last iteration
const userHasOrgPermission = await self._bouncer.doesUserHavePermission(null, requestingUserId, DatabaseIds.enum__permissions__canViewAllRolesOfAnyUseratThisFranchisor, role.franchisorId, null)
if (userHasOrgPermission) result = true;
}
})
}
//user manages a franchisee the requestee is part of
if (!result) {
requestedUsersRoles.forEach( async (role) => {
if (!result) { //may have found it last iteration
const userHasOrgPermission = await self._bouncer.doesUserHavePermission(null, requestingUserId, DatabaseIds.enum__permissions__canViewAllRolesOfAnyUseratThisFranchisor, null, role.franchiseeId)
if (userHasOrgPermission) result = true;
}
})
}
if (!result)
hapiResolve(responseHelper.getErrorResponse(ResponseErrorCode.notAuthorised));
resolve(result);
}
catch (error) {
console.trace(error);
reject(error);
}
});
}
doesUserHavePermission(hapiResolve: (response: any) => void, userId: string, permissionId: string, franchisorId: string, franchiseeId: string): Promise<boolean> {
return new Promise<boolean>( async (resolve, reject) => {
try {
const roles = await this._rolesQueries.getRolesAndPermissionsForUser(userId);
if (!this.doRolesHavePermission(roles, permissionId, franchisorId, franchiseeId)) {
if (hapiResolve)
hapiResolve(responseHelper.getErrorResponse(ResponseErrorCode.notAuthorised));
resolve(false);
}
else resolve(true);
}
catch (error) {
if (hapiResolve)
hapiResolve(responseHelper.getErrorResponse(ResponseErrorCode.unknownError));
reject(error);
}
});
}