0
    **login.js**

    var obj = require("../config/db_properties");
    var conn = require("../config/db_connection");
    var connection = conn.getConnection();
    connection.connect();
    var express = require("express");
    var router = express.Router();
    var myFun = require("../token/token")
    router.post("/",(req,res)=>{
        var uname = req.body.uname;
        var upwd = req.body.upwd;
        connection.query("select * from login_details where uname='"+uname+"' and upwd='"+upwd+"'",
        (err,recordsArray,fields)=>{
            if(recordsArray.length>0){
              var token = myFun("HELLO","JKBDSJJL");
              obj.token = token;
              res.send({"login":"success","token":token});
            }else{
                res.send({"login":"fail"});
            };
        });
    });
    module.exports = router;



    **loginservice.ts**


    import { Injectable } from '@angular/core';
    import { HttpClient } from '@angular/common/http';

    @Injectable({
      providedIn: 'root'
    })
    export class LoginService {   

      constructor(private _http:HttpClient) { }
      authentication(obj:any){
        return this._http.post("http://localhost:8080/login",obj)
      }
    }




    logincomponent.ts

    import { Component, OnInit } from '@angular/core';
    import { LoginService } from 'src/app/services/login.service';
    import { Router } from "@angular/router";
    import { HttpErrorResponse } from '@angular/common/http';

    @Component({
      selector: 'app-login',
      templateUrl: './login.component.html',
      styleUrls: ['./login.component.css']
    })

    export class LoginComponent implements OnInit {

      constructor(private _service:LoginService,private _router:Router) { }
      ngOnInit() {
      }

      public login(obj:any):any{
        this._service.authentication(obj).subscribe(
          (posRes)=>{
            if(posRes.login == "success"){
              window.localStorage.setItem("login_details",JSON.stringify(posRes));
              this._router.navigate(["/dashboard"]);
            }
          },
          (err:HttpErrorResponse)=>{
            if(err.error instanceof Error){
              console.log("client side error")
            }else{
              console.log("server side error")
            }
          }
        )
      }
    }

**login.component.html**

<label style="margin-right:100px">Uname.</label>
<input type="text" [(ngModel)]="uname">
<label style="margin-right:100px">upwd.</label>
<input type="password" [(ngModel)]="upwd">
<br><br>
<button (click)="login({'uname':uname,'upwd':upwd})">login</button>

here i am creating a token based login application. i am getting this error after i run my server and angular app

ERROR in src/app/components/login/login.component.ts(21,19): error TS2339: Property 'login' does not exist on type 'Object'. can any one solve this error...?

The Head Rush
  • 2,522
  • 2
  • 21
  • 39
raj
  • 11
  • 1
  • 6

2 Answers2

0

you can try these way

if(posRes['login'] == "success"){}
Bhagwat Tupe
  • 1,663
  • 1
  • 8
  • 25
0

This is probably because posRes doesn't have an object login , probably you need posRes.data.login, not sure how your backend is setup

you should try adding these console statements to check

          (posRes)=>{
        console.log('typeof posRes',typeof posRes); // should be Json if not do Json.parse()
        console.log('posRes',posRes); // should print Json object
        console.log('posRes.login',posRes.login); // should have data other wise wrap below if with another if(posRes.login){
                if(posRes.login == "success"){
                  window.localStorage.setItem("login_details",JSON.stringify(posRes));
                  this._router.navigate(["/dashboard"]);
                }
dota2pro
  • 5,432
  • 5
  • 25
  • 52