Property 'present' does not exist on type '(message?: any) => void'


#1

Hi everyone,

I’m trying to display an alert prompt. I imported AlertController from ionic-angular and registered alertCtrl in my constructor.

But I’m getting this error when trying to compile my program.

Property ‘present’ does not exist on type ‘(message?: any) => void’.

I copied/pasted the official code from the ionic documentation so I can’t figure out what I’m doing wrong.

Here’s my code:

import { Alert, AlertController } from 'ionic-angular';

constructor(
    platform: Platform,
    private fb: Facebook,
    private device: Device,
    private storage: Storage,
    private navCtrl: NavController,
    private alertCtrl: AlertController,
    private userService: NmUserService,
    private deviceService: NmDeviceService
  ) { }

presentPrompt(flag: boolean) {
    if (flag == true) {
      let alert = this.alertCtrl.create({
        title: 'Pseudo déjà utilisé',
        inputs: [
          {
            name: 'username',
            placeholder: 'Ce pseudo est déjà utilisé, choisissez-en un autre.'
          }
        ],
        buttons: [
          {
            text: 'Cancel',
            role: 'cancel',
            handler: data => {
              console.log('Cancel clicked');
            }
          },
          {
            text: 'Confirmer',
            handler: data => {
              this.token.user.username = data.username;
              this.userService.update(this.token.user, this.token).subscribe(
                (result) => {
                  this.navCtrl.push(StaffsPage);
                },
                (error) => {
                  this.presentPrompt(true);
                }
              )
            }
          }
        ]
      });
    }
  alert.present(); // The error is here.

Thanks to anyone that will take the time to read and/or help me.


#2

The alert that you are apparently attempting to access is inside the scope of the if (flag) block, but your access attempt is outside it. Move the present() line up above the closing } that is currently above it.


#3

Thanks, I didn’t know the difference between let and var :slight_smile: