Error Found in Infinite Scroll Functionality in Ionic 2

I have been attempting to incorporate infinite scrolling into my application, but I keep encountering an error that says

Property "then" does not exist on type "void"
. This issue appears in both my text editor and the console when the page reaches the bottom. The error seems to be related to a http get request which is fetching data.

perpage:number = 50;

loadCategory1(start:number=0) {
  var url = "http://api.yummly.com/v1/api/recipes?_app_id=////&_app_key=////";

  if (this.Category1) {
    return Promise.resolve(this.Category1);
  }

  return new Promise(resolve => {
    this.http.get(url + "&allowedAllergy[]=396^Dairy-Free&allowedAllergy[]=393^Gluten-Free&maxResult=" + this.perpage + "&start=" + start)
      .map(res => res.json())
      .subscribe(data => {
        console.log(data);
        this.Category1 = data.matches;
        resolve(this.Category1);
      });
  });
}

The code snippet associated with this problem is as follows:

export class Category1Page {
  public api: any;
  private start:number=0;

  constructor(public navCtrl: NavController, public navParams: NavParams, public apiAuthentication: ApiAuthentication) {
    this.loadRecipes();
  }

  loadRecipes(){
    this.apiAuthentication.loadCategory1(this.start)
    .then(data => {
      this.api = data;
    });
  } 

  doInfinite(infiniteScroll:any) {
     console.log('doInfinite, start is currently '+this.start);
     this.start+=50;

     this.loadRecipes().then(()=>{
       infiniteScroll.complete();
     });
  }
}

In the HTML section:

 <ion-infinite-scroll (ionInfinite)="doInfinite($event)">
   <ion-infinite-scroll-content></ion-infinite-scroll-content>
 </ion-infinite-scroll>

If anyone has any advice or suggestions, it would be greatly appreciated. Thank you.

Answer №1

An error occurred: Property "then" cannot be called on type "void"

The issue is that your loadRecipes method is not returning any value (it's void) but you are attempting to use then() on it.

this.loadRecipes().then(()=>{
       infiniteScroll.complete();
     });

To fix this, make sure you add the return keyword like so:

loadRecipes(){
    return this.apiAuthentication.loadCategory1(this.start)
    .then(data => {
      this.api = data;
    });
  } 

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

Material-UI Alert: The property `onKeyboardFocus` for event handling is unrecognized and will not be applied

Here is a more detailed trace of the issue: warning.js:33 Warning: Unknown event handler property `onKeyboardFocus`. It will be ignored. in div (created by IconMenu) in div (created by IconMenu) in IconMenu (created by DropdownMenu) in div ...

What is causing this issue in TypeScript version 4.8?

After updating to TypeScript 4.8 in VSCode, I have encountered an error in one of my React projects that was not present before. Strangely, this error does not prevent the code from compiling successfully when building the project. It's challenging to ...

Tips for implementing a feature in Angular 6 that enables an input box to accept both negative and positive values within the range of 0 to

HTML markup <input type="number" min="0" max="100" required placeholder="Charge" [(ngModel)]="rateInput" name="rateInput" [formControl]="rateControl"> Implementing TypeScript validation this.rateControl = new FormControl("", [Validators.max(100) ...

Don't continue to expect other promises if one of them comes back with a special

I am dealing with a situation where I need to validate data in multiple tables by utilizing a function called validateTables(). This function relies on an asynchronous helper function queryTable() to check each table through API queries. The requirement fo ...

if the condition is not met, proceed to the alternate template eventually

Hey there, I'm facing an issue with my code that resembles something like this: <ng-container *ngIf="firstCondition; else Farewell"> <ng-container *ngIf="innerContainer"> <div class="Hello Message"> {{HelloMessa ...

One should refrain from loading the API in Angular when there is no data present, by utilizing the global.getData method

Check out this code snippet: loadNextBatch() { console.log('scrolldown'); this.pageIndex = this.pageIndex + 1; this.global.getData(`/conditions/latest?start=${this.pageIndex}&length=${this.pageSize}`) .pipe(take(1)).subscr ...

After redirection to a new page, the Ionic-vue menu malfunctioned

I have developed a reusable header component named Header, which contains an IonMenu component for consistency across pages. The menu is associated with the IonRouterOutlet as per the documentation, but I am encountering an issue when navigating between pa ...

Seasonal calendar selector

Currently, I am searching for a Quarterly date picker utilizing ng-bootstrap. Right now, I already have a Month and Year picker, as shown in this STACKBLITZ, but my goal is to switch the Month to Quarter. https://i.sstatic.net/1kWN3.png Can ng-bootstrap ...

Tips for minimizing the amount of code in Angular 5

As our Angular 5 application continues to expand, due to being a Single Page Application with dynamic components, our main component is becoming overloaded. The main component is responsible for rendering other components and contains the majority of the l ...

Angular material raised button with the file input feature for versions 4 and 5

I am currently developing an Angular application and for file upload, I am using the following code snippet: <label class="btn btn-default"> <input type="file" (change)="selectFile($event)"> </label> <button class="btn btn-success ...

How about developing an application using Ionic2 and Angular2 for the frontend, combined with ASP.NET Core for the backend

Is it possible to develop an app using Ionic2-Angular2 for the front-end and ASP.NET Core for the back-end on the same server? And how can we ensure that the application is multi-platform compatible? ...

Angular 2: What is the reason for preventing the use of subscribe on the Subscriber object?

If I have an observable object o : let o: Observable<Object> = ... I am able to subscribe to this object, but why is it not permitted to subscribe to the Subscriber object? To illustrate with a real-life example: myServiceCall() { let o: O ...

After adding the exclude property to angular-cli.json, the spec files are now being linted

I am working on an Angular 4 application and have manually created two .spec files for a specific class. When I use the nglint command, it successfully lints these two files. However, the spec files that were automatically generated when I created a compon ...

Tips for updating Ref objects in React

In the process of fixing a section of my project, I'm encountering an issue where I have no control over how refs are being utilized. The Editable text elements are currently handled through refs and a state variable within the component that holds al ...

Webpack is failing to recognize certain CSS files

My development stack includes Vue.js 2.5.15, Webpack 4.12.0, css-loader 0.28.11, ASP.Net Core 2.1 in Visual Studio 2017. Starting with the Visual Studio asp.net core template project for Vue and Typescript, I prefer to have individual small CSS files with ...

I'm puzzled as to why my get request seems to be hit-or-miss, working at times but returning a 404 not found error at other

Currently in the process of learning Angular and working on a project that involves a MongoDB database and Express for APIs. I am trying to retrieve comments for a specific post using the post ID. The GET request should return a list of comments, but the ...

Exploring the concept of using a single route with multiple DTOs in NestJS

At the moment, I am utilizing NestJS for creating a restful API. However, I am currently facing an issue with the ValidationPipe. It seems to only be functioning properly within controller methods and not when used in service methods. My goal is to implem ...

Unexpected Failure in Angular 7 Compilation

My angular app was functioning perfectly, but suddenly it stopped compiling. I received the following message: ng build --prod 10% building modules 3/6 modules 3 active ...ogress\kendo-theme-default\dist\all.cssBrowserslist: caniuse-lite i ...

Executing a particular end-to-end test case using Angular 2, Karma, and Protractor

Is there a specific command I can use to run an individual e2e test case from my test suite? If that's not possible, is there some workaround for running a specific test suite? I'm currently using Jasmine, Karma, and Protractor. To start my tes ...

Finding the Device's Memory and Storage in Android: A Guide

I am currently facing an issue with retrieving the accurate memory and storage information of my device using the cordova-plugin-extended-device-information. The plugin seems to only provide this data once at deviceReady event and does not update it later ...