Typescript error cannot find name: showalert


#1

ive used the alert controller before. and used them within a function but this is the first time im encountering this error.

here is my home.ts file

import { Component } from '@angular/core';
import { BluetoothSerial } from 'ionic-native';
import { NavController } from 'ionic-angular';
import { AlertController } from 'ionic-angular';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'

})
export class HomePage {
devices: any;

constructor(public alertCtrl: AlertController, public navCtrl: NavController){}

 search(){
    this.devices = BluetoothSerial.list().then(
      function(data) {
        console.log("hurray" + data);
        showAlert("Found Devices", data);
      },
      function(error) {
      console.log("could not find paired devices because: " + error);
      showAlert("Error", error);
    });
  }

  showAlert(title, info) {
    let alert = this.alertCtrl.create({
      title: title,
      message: info,
      buttons: ['OK']
    });
    alert.present();
  }
}

also any advice on my Bluetooth code would also be a helpful added bonus.

thankyou for your time


#2

inside you search();

always use the “this”, should be:
this.showAlert("text here",data);


#3

The advice in the previous post won’t work unless you also follow a very important heuristic: never type the word “function” inside of one. Always use arrow functions. You should also give devices a proper type instead of any.


#4

proplem is i dont know what type is should be though.


#5

thankyou both the alerts show now. so i changed my code to.

import { Component } from '@angular/core';
import { BluetoothSerial } from 'ionic-native';
import { NavController } from 'ionic-angular';
import { AlertController } from 'ionic-angular';
import { BLE } from '@ionic-native/ble';


@Component({
  selector: 'page-home',
  templateUrl: 'home.html'

})
export class HomePage {
devices: any;

constructor(public alertCtrl: AlertController, public navCtrl: NavController){}

 search(){
    this.devices = BluetoothSerial.list().then((data) => {
        console.log("hurray" + data);
        this.showAlert("Found Devices", data);
      },
      (error) => {
      console.log("could not find paired devices because: " + error);
      this.showAlert("Error", error);
    });
  }

  showAlert(title, info) {
    let alert = this.alertCtrl.create({
      title: title,
      message: info,
      buttons: ['OK']
    });
    alert.present();
  }
}