TypeError: Cannot read property 'openModalDialog' of undefined

Viewed 30

this is my .ts file, when ever if condition fails and comes to else i want to display the request modal box so there is a function openModalDialog() , but it is showing undefined.

please help me thanks in advance

import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal';
import { RequestModalComponent } from '../../request-modal/request-modal.component';
import axios from 'axios';

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

  export class SharedOndatetimeScheduledComponent implements OnInit {
    bsModalRef: BsModalRef;
    constructor( private modalService: BsModalService ) { }

    ngOnInit(): void {
    } 

   openModalDialog() {
    console.log("callling this method.......")
     this.bsModalRef = this.modalService.show(RequestModalComponent);
   }

 navigateToTeleApp = () => {
   axios.get("*******").then(function (res) {
  console.log("response ...", res)
  if (res.data.message == "Not Found"){
    console.log("this condition....")
  }       
  else {
    console.log("this condition....222");
    this.openModalDialog();  
  }
  });
 }
1 Answers

i think its because get method from axis where you using function keyword to handle a callback and its not bnded so this getting undefined try to use lamda function over there.

    navigateToTeleApp = () => {
// below line changed from function to lamda function
       axios.get("*******").then((res) => {
      console.log("response ...", res)
      if (res.data.message == "Not Found"){
        console.log("this condition....")
      }       
      else {
        console.log("this condition....222");
        this.openModalDialog();  
      }
      });
     }
Related