未收到对象时比较值(TypeScript、Aurelia)
Compare values when object not received (TypeScript, Aurelia)
我正在尝试比较已定义的 role
(在我的 app.ts
中)和从 WebServer
恢复的角色。但是当我没有登录时,我在比较值时遇到问题:
这就是我正在做的事情:
export class RoutingAuthorizeStep {
public readonly userIdentity: UserIdentityModel;
constructor (userIdentity: UserIdentityModel) {
this.userIdentity = userIdentity;}
run(navigationInstruction: NavigationInstruction, next: Next) : Promise<any> {
let requiredRoles = navigationInstruction.getAllInstructions()
.map(i => i.config.settings.roles)[0] as string[];
//requiredRoles is example 'superUser'
let isUserInRole = requiredRoles?
requiredRoles.some(r => r === this.userIdentity.role) : true;
}
}
当我检查调试时:
console.log(this.userIdentity.role);
我有这条消息:
aurelia-logging-console.js?dc89:45 ERROR [app-router] TypeError:
Cannot read property 'role' of undefined
at RoutingAuthorizeStep.run (routing-authorize-step.ts?008f:30)
at next (aurelia-router.js?e32b:433)
我不是 Aurelia 开发人员,但这似乎是一个简单的 JS 问题,除非我错了。假设错误在 "isUserInRole" 中,您可以这样做。
let isUserInRole = requiredRoles?
requiredRoles.some(r => this.userIdentity && r === this.userIdentity.role) : true;
}
基本上只需在检查其角色之前检查您的 userIdentity 是否存在。
希望对您有所帮助!
我解决了这个原因:
run(navigationInstruction: NavigationInstruction, next: Next) : Promise<any> {
if (this.userIdentity == null)
{
//return to login
}
else
{
let requiredRoles: string;
requiredRoles = navigationInstruction.getAllInstructions()
.map(i => i.config.settings.roles)[0];
if (requiredRoles === this.userIdentity.role)
{
return next();
}
//return to login
}
}
它正在工作。但 requiredRoles.some
仍然是问题 - 可能 library is missing
.
我正在尝试比较已定义的 role
(在我的 app.ts
中)和从 WebServer
恢复的角色。但是当我没有登录时,我在比较值时遇到问题:
这就是我正在做的事情:
export class RoutingAuthorizeStep {
public readonly userIdentity: UserIdentityModel;
constructor (userIdentity: UserIdentityModel) {
this.userIdentity = userIdentity;}
run(navigationInstruction: NavigationInstruction, next: Next) : Promise<any> {
let requiredRoles = navigationInstruction.getAllInstructions()
.map(i => i.config.settings.roles)[0] as string[];
//requiredRoles is example 'superUser'
let isUserInRole = requiredRoles?
requiredRoles.some(r => r === this.userIdentity.role) : true;
}
}
当我检查调试时:
console.log(this.userIdentity.role);
我有这条消息:
aurelia-logging-console.js?dc89:45 ERROR [app-router] TypeError:
Cannot read property 'role' of undefined
at RoutingAuthorizeStep.run (routing-authorize-step.ts?008f:30)
at next (aurelia-router.js?e32b:433)
我不是 Aurelia 开发人员,但这似乎是一个简单的 JS 问题,除非我错了。假设错误在 "isUserInRole" 中,您可以这样做。
let isUserInRole = requiredRoles?
requiredRoles.some(r => this.userIdentity && r === this.userIdentity.role) : true;
}
基本上只需在检查其角色之前检查您的 userIdentity 是否存在。
希望对您有所帮助!
我解决了这个原因:
run(navigationInstruction: NavigationInstruction, next: Next) : Promise<any> {
if (this.userIdentity == null)
{
//return to login
}
else
{
let requiredRoles: string;
requiredRoles = navigationInstruction.getAllInstructions()
.map(i => i.config.settings.roles)[0];
if (requiredRoles === this.userIdentity.role)
{
return next();
}
//return to login
}
}
它正在工作。但 requiredRoles.some
仍然是问题 - 可能 library is missing
.