Error: Attempting to access the 'presence' property of an undefined value

Having trouble with calling loading/Toast/Alert in Ionic2 under the current scenario. Being a newcomer to Ionic development, I'm struggling to understand it. I realize it's a trivial mistake.

 var dg = document.getElementById('btnregis');
   dg.onclick =()=> this.presentLoading();


   presentLoading() {
    console.log("Registered");
    let loading = Loading.create({
        content: "Please wait...",
        duration: 3000
    });
    this.navController.present(loading);
    this.Reg_success();
}


    Reg_success() {
console.log("registration success");
        this.Billerlabelview = false;
        let toast = Toast.create({
            message: "Registering...",
            duration: 3000
        });
        this.navController.present(toast);
    }

The reg_Success() function isn't being triggered. It results in a similar error. Could there be something that I'm overlooking?

Answer №1

When can you find the navController in action? You have the option to connect the db.onclick method with it.

var dg = document.getElementById('btnregis');
 dg.onclick = this.launchRegistrationLoading.bind(this);

launchRegistrationLoading() {
    console.log("Registered");
    let loading = Loading.create({
        content: "Please wait...",
        duration: 3000
    });
    this.navController.present(loading);
}

Answer №2

When you pass the reference to a function, the this context is not available.

It is recommended to use lambdas (or fat arrow functions) when passing functions as parameters. This way, you can maintain the correct this context from the relevant class/scope, making your code more readable and consistent.

dg.onclick = () => this.presentLoading();

For further insights on the usage of lambdas, check out:

Answer №3

Before anything else, make sure that you have properly set up the present() method in your constructor when using it with NavController:

class MyClass{
   constructor(navController: NavController) {
      // To ensure clarity and understanding, follow this recommended line from Ionic docs http://ionicframework.com/docs/v2/2.0.0-beta.9/api/components/nav/NavController/#present
      this.navController = navController;
   }    
}

In response to your inquiry, as indicated in the Ionic documentation, the present() function returns a promise. Therefore, you should wait for its resolution before proceeding to invoke your Reg_success method:

presentLoading() {
    console.log("Registered");
    let loading = Loading.create({
        content: "Please wait...",
        duration: 3000
    });
    this.navController.present(loading).then(() => {
        this.Reg_success();
    });        
}

Additionally, consider optimizing how you handle the click event. Instead of setting it like this:

dg.onclick = this.presentLoading.bind(this);

You can adopt the more streamlined approach in Angular/Ionic by incorporating this snippet within your button tag:

<button (click)="presentLoading()">Click me!</button>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to eliminate the dotted line from the image map in Internet Explorer 11

Below you will find the code I am working with: <img alt="Testing 2015" border="0" src="images/Test-2015.jpg" usemap="#Map" /> <p><map name="Map"><area coords="790,100,653,135" href="http://www.google.com/" shape="rect" style="cursor ...

Retrieving the value of a <select> element using React.useState in a Nextjs environment

Encountering an issue in Nextjs involving the useState function from React. There is a select element with multiple options. Upon selection, the useState should store the value of the selected option. const [value, setValue] = useState('') ... ...

Is there a way to imitate a method that initiates an AJAX request?

I am currently working on writing tests for my Angular application and I need to mock a method in order to avoid making actual requests to the server. Within my grid.service.ts file, here is the method I am trying to mock: loadAccountListPromise(id: str ...

Vertical Orientation in HTML

Would appreciate some assistance on creating a vertical text with HTML similar to the example in the linked screenshot. It needs to be straightforward and vertically oriented. Check out the screenshot for reference ...

What is preventing form validation from functioning properly with both built-in and cross-field validators?

I've been facing issues while trying to develop a form with built-in validators and cross-field validator. Unfortunately, the functionality is not working as expected and I'm struggling to understand why. The form should contain four types of bu ...

Utilize Javascript to extract real-time information from MySQL database

In my project, I have a MySQL database where I fetch data using Node JS. Additionally, I utilize AngularJS for live searching capability. Below is a snippet of my HTML code: <div class="songlist" ng-controller='another_control'> ...

What is the method in JavaScript to display the information stored in a title attribute?

Can anyone help me with this code snippet I have? <i class="my-class" title="AB"> ::before </i> I need to extract and output the value of the title attribute, which is AB Example output should be: <span class="my-class">AB</spa ...

Effective strategies for organizing component features in React

As I was reading through the React documentation, I came across the idea that using React effectively involves following the Single Responsibility Principle, meaning each component should have a specific purpose. I've already created a basic Gameboard ...

Unable to subscribe due to the return value being an AnonymousSubject rather than an Observable

I am facing an issue in Angular 4 where I am attempting to retrieve details from a specific user when clicking on the 'user link profile'. However, I am unable to subscribe to my function because the return value of switchMap is AnonymousSubject ...

How to handle redirects based on status codes in AngularJS $http requests

My angular application includes numerous $http requests, and I am looking for a way to automatically redirect users to the login page in case the server session expires (resulting in a 401 error). Does anyone have a solution that can handle this for all ...

Understanding DefinitelyTyped: Deciphering the explanation behind 'export = _;'

Having trouble integrating angular-material with an ng-metadata project and encountering some issues. Utilizing DefinitelyTyped for angular material, the initial lines are as follows: declare module 'angular-material' { var _: string; expo ...

Attempting to connect JQuery to a different page through a data attribute connection

I am currently working on a slider that connects slides from an external page through data attributes. Here is the setup: Main Page <div id="sitewrapper"> <section class="sliderwrapper"> <div id="slider" data-source="slides-pa ...

Adjusting value of one input field according to the content of another text field in an

Just starting out with Angular and I am looking to dynamically change the value of a hidden input field called 'cc_card' based on the first digit entered in another input field called 'cc_number'. For example, if the user enters 5 in &a ...

Revise/Add ng-submit and service for an enhanced system

I am attempting to use ng-submit to update or create new posts in the database. Below is the HTML code I am using: <form ng-submit="savePost()"> <input ng-model="post.TASK"> <input ng-model="post.STATUS""> <but ...

I am looking to attach a Bootstrap 4 class to the form input field

I needed to apply the "is-invalid" bootstrap4 class when my Angular form field was invalid and touched. I attempted to achieve this with: <input type="text" #name class="form-control" [class.is-invalid]="name.invalid && name.touched" name="Name ...

Dependency of multiple objects in Webgl three.js

I'm struggling with object dependencies and need help. I want to create a setup where one object is dependent on two other objects. Essentially, when I modify the position of one parent object (like changing the y-Position), the dependent object (chil ...

Tips for displaying Vue Components on an HTML5 canvas surface

How can I incorporate an htmlcanvas as the webpage background and overlay Vuejs components on top of it? I know the answer must exist, but I'm not sure where to start looking. ...

Guide on creating a line chart resembling the one in Highcharts library

I would like to create a line chart using the HighChart library, using data similar to what is shown in the image below. However, I am not familiar with HTML, CSS, or JavaScript and don't know how to go about developing this. Can anyone provide guidan ...

Create a package that is compatible with both node and browser environments

After successfully creating a node NPM package that acts as a wrapper for a specific JSON API using node's http, https, and querystring modules, I am now looking to expand its usability by making it compatible with browsers. This would involve replaci ...

What causes my ready function to consistently execute upon controller or directive reinitialization?

Every time my controller or directive reinisilize, the angular.element(document).ready(function(){...}); function is executed. Why does this happen? In my scenario, I have two controllers and one directive. I update a value in the parent controller which ...