Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
407 views
in Technique[技术] by (71.8m points)

javascript - 类型不存在属性。 怎么了(Property doesn't exist on type.. error. What is wrong here?)

I'm posting here hoping one of you could help me.(我在这里发布,希望你们中的一个可以帮助我。)

I have a very little of programming knowledge and I'm being asked to put together a simple e-commerce website in Angular and ASP.NET Core MVC 3. So the following code is supposed to not log the user out if he/she is logged in but tries to access the admin's page through url.(我只有很少的编程知识,所以我被要求在Angular和ASP.NET Core MVC 3中建立一个简单的电子商务网站。因此,以下代码应该不会使用户注销:登录但尝试通过url访问管理员页面。) This is a bit of code that my professor told us to use but for some reason it doesn't work for me.(这是我的教授告诉我们使用的一些代码,但由于某种原因,它对我不起作用。) He's using an older version of Angular, so for example, where he uses .map(), I have to use .pipe(map()) and where he had response.json().role I did just response.role.(他使用的是Angular的旧版本,因此,例如,在他使用.map()的情况下,我必须使用.pipe(map()),而在他使用response.json()。role的地方,我只做了response.role。) My IDE is showing me the following error in the client side login method around response.authenticated and response.role(我的IDE向我显示了围绕response.authenticated和response.role的客户端登录方法中的以下错误) Property 'authenticated' (or 'role') does not exist on type 'boolean'.(类型“ boolean”上不存在“已认证”(或“角色”)属性。) I suspect the problem is with returning multiple arguments on the client side login method or it's caused by the .pipe(map()).(我怀疑问题出在客户端登录方法上返回多个参数,或者是由.pipe(map())引起的。) I'm clueless here, to be honest.(老实说,我在这里一无所知。) I'd appreciate any guidance(我将不胜感激) login:(登录:) login(): Observable<any> { this.authenticated = false; return this.repo.login(this.name, this.password).pipe( map(response => { if (response.authenticated == 'true') { this.authenticated = true; this.password = null; this.role = response.role; if (this.role == 'Administrator') { this.router.navigateByUrl("/admin/overview"); this.admin = true; } else { this.router.navigateByUrl("/"); } } return this.authenticated; }), catchError(e => { this.authenticated = false; return of(false); }) ); } server side login:(服务器端登录:) [HttpPost("/api/account/login")] public async Task<IActionResult> Login([FromBody] LoginViewModel creds) { if (ModelState.IsValid && await DoLogin(creds)) { IdentityUser user = await userManager.FindByNameAsync(creds.Name); if (await userManager.IsInRoleAsync(user, "Administrator")) return Ok(new { authenticated = "true", role = "Administrator"} ); else return Ok( new { authenticated = "true", role = "NoAdministrator" }); } return BadRequest(); }   ask by zueyl translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The error is very clear.(错误非常明显。)

response is not an object to access its members (authenticate).(response不是访问其成员(身份验证)的对象。) This returns boolean:(这将返回布尔值:) this.repo.login(this.name, this.password); Change your condition to:(将您的条件更改为:) response == 'true' OR(要么) According to your backend, the response is an object.(根据您的后端,响应是一个对象。) Change this.repo.login return type to any.(将this.repo.login返回类型更改为any。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...