正如您在下面的.gif中所看到的,我第一次调用数据库时它会成功返回数据。但接下来的两次它既没有返回也没有错误,异步代码就消失了。更糟糕的是,我的等待声明不等待响应,它只是继续。
有谁能告诉我为什么随后的Sequelize电话会消失?更重要的是,即使我使用await
?
这是最里面的代码:
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); }
});
}
呼叫:
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);
}
});
}