简体   繁体   English

类型上不存在属性.. 错误。 这里有什么问题?

[英]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()).我怀疑问题出在客户端登录方法上返回多个参数,或者是由 the.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();
        }

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返回类型更改为任意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型“ {}”上不存在属性“更新”和“数量” - Property 'update' and 'quantity' doesn't exist on type '{ }' 字符串类型不存在属性 | 目的 - Property doesn't exist on type string | object 每次运行 function 时都想返回图像,但它一直给我一个错误。 这里出了什么问题? - Want to return an image everytime the function is run but it keeps giving me an error. What's wrong here? “$.______ 不是函数”错误。 怎么了? - “$.______ is not a function” error. What is wrong? Angular 8 错误。 类型上不存在“显示” - Angular 8 error. ‘show’ does not exist on type 从服务订阅数据时,类型 void 上不存在错误属性订阅? - error property subscribe doesn't exist on type void when subscribe data from service? 属性不存在 - Property doesn't exist 错误:“&#39;JSON&#39;类型不存在属性&#39;.get&#39;(HTML / Javascript / Angular2 / Typescript) - Error: "Property '.get' doesn't exist on type 'JSON' (HTML / Javascript / Angular2 / Typescript) 在用户登录时访问Google联系人会引发Meteor.bindEnvironment错误。 我对这里的纤维的理解有什么问题? - Accessing google contacts on user-sign-in throws Meteor.bindEnvironment error. What is wrong with my understanding of the fibers here? msSaveOrOpenBlob 属性在 Navigator 类型上不存在 angular 13 - msSaveOrOpenBlob property doesn't exist on type Navigator angular 13
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM