Error: The Ionic and WordPress integration encountered an issue because the property 'length' is undefined

I am trying to establish a connection between Ionic and WordPress API with Angular. However, I am encountering the following error:

TypeError: Cannot read property 'length' of undefined

at WpProvider.webpackJsonp.211.WpProvider.getUserImage (http://localhost:8100/build/main.js:293:51)
at BlogPage.webpackJsonp.150.BlogPage.getUserImage (http://localhost:8100/build/main.js:36:32)
at Object.eval [as updateRenderer] (ng:///AppModule/BlogPage.ngfactory.js:47:25)
at Object.debugUpdateRenderer [as updateRenderer] (http://localhost:8100/build/vendor.js:15041:21)
at checkAndUpdateView (http://localhost:8100/build/vendor.js:14177:14)
at callViewAction (http://localhost:8100/build/vendor.js:14522:21)
at execEmbeddedViewsAction (http://localhost:8100/build/vendor.js:14480:17)
at checkAndUpdateView (http://localhost:8100/build/vendor.js:14173:5)
at callViewAction (http://localhost:8100/build/vendor.js:14522:21)
at execComponentViewsAction (http://localhost:8100/build/vendor.js:14454:13)

What could be causing this issue?

Answer №1

There seems to be an issue with the code in wp.js using the provider wp-api-angular. The code and obj are not aligned:

import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map';
import { Observable } from 'rxjs/Observable';
import { WpApiPosts, WpApiMedia, WpApiUsers } from 'wp-api-angular';
import { Http, HttpModule } from '@angular/http';

export class Post {
  public media_url: Observable<string>;
  constructor(public authorId: number, public id: number, public title: string, public content: string, public excerpt: string, public date: string, public mediaId?: number) { }
}

export class User {
  constructor(public id: number, public name: string, public userImageUrl: string) { }
}

@Injectable()
export class WpProvider {

  users: User[];

   constructor(public wpApiPosts: WpApiPosts, public wpApiMedia: WpApiMedia, public wpApiUsers: WpApiUsers) {
     this.wpApiUsers.getList()
       .map(res => res.json())
       .subscribe(data => {
         this.users = [];
         for (let user of data) {
           let oneUser = new User(user[ 'id' ], user[ 'name' ], user[ 'avatar_urls' ][ '96' ]);
           this.users.push(oneUser);
         }
       })
   }

   getPosts(): Observable<Post[]> {
    return this.wpApiPosts.getList()
      .map(res => res.json())
      .map(data => {
        var posts = [];
        for (let post of data) {
          let onePost = new Post(post[ 'author' ], post[ 'id' ], post[ 'title' ][ 'rendered' ], post[ 'content' ][ 'rendered' ], post[ 'excerpt' ][ 'rendered' ], post[ 'date' ], post[ 'featured_media' ]);
          onePost.media_url = this.getMedia(onePost.mediaId);
          posts.push(onePost);
        }
        return posts;
      });
  }

  getMedia(id: number): Observable<string> {
    return this.wpApiMedia.get(id)
      .map(res => res.json())
      .map(data => {
        return data[ 'source_url' ];
      });
  }

  getUserImage(userId: number) {
    for (let usr of this.users) {
      if (usr.id === userId) {
        return usr.userImageUrl;
      }
    }
  }

  getUserName(userId: number) {
    for (let usr of this.users) {
      if (usr.id === userId) {
        return usr.name;
      }
    }
  }

}

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

Encountered a PHP error in WordPress - Alert: Attempting to access an array offset on a value of type boolean within the plugins/Ultimate_VC_Addons/.. file at

I'm encountering a problem with my WordPress website that is displaying a notice and causing a major issue on my site. When I visit the homepage, I see this message: Trying to access array offset on value of type bool in /www/wp-content/plugins/Ult ...

Aurelia TypeScript app experiencing compatibility issues with Safari version 7.1, runs smoothly on versions 8 onwards

Our team developed an application using the Aurelia framework that utilizes ES6 decorators. While the app works smoothly on Chrome, Firefox, and Safari versions 8 and above, it encounters difficulties on Safari 7.1. What steps should we take to resolve th ...

Another term for the *ngIf directive is as follows:

Many years back, the documentation for Angular 2 mentioned a different name for *ngIf that did not include an asterisk. It was something along the lines of <prefix>-ng-if, where <prefix> represented a short prefix. Unfortunately, I could not l ...

Troubleshooting problem with sorting in Angular 4 material header

Using Angular 4 material for a table has presented me with two issues: 1. When sorting a table, it displays the description of the sorting order in the header. I would like to remove this. https://i.sstatic.net/5bHFO.png It displays "Sorted by ascending o ...

The Angular application has encountered a stack overflow due to exceeding the maximum

./src/main.ts - An issue occurred: The module build process failed (from ./node_modules/@ngtools/webpack/src/ivy/index.js): Error: Maximum call stack size exceeded import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; { App ...

Having difficulty showing the ionic back button within my side menu

I recently added an ion-nav-back-button to my side menu in the ion-nav-bar, but I encountered an issue where the nav back button did not display. Despite trying various attributes, the button remains hidden. Below is the code for my menu layout with the si ...

Typescript: Verifying the type of an interface

In my code, I have a function called getUniqueId that can handle two different types of interfaces: ReadOnlyInfo and EditInfo. Depending on the type passed to this function, it will return a uniqueId from either interface: interface ReadOnlyInfo { item ...

The type 'Data' is lacking the following attributes from its definition

Being a newcomer to Angular 8, I can't figure out why this error is popping up. If you have any suggestions on how to improve the code below, please feel free to share your tips. The error message reads: Type 'Data' is missing the follo ...

Different property 'responseType' types are not compatible

Having trouble making a POST request in Angular 5 that accepts text/plain as a response. The method being called in Angular expects a JSON response, causing an error when trying to parse the response. Attempted to call the method with parameter {responseT ...

Angular: Modify parent component's field using route

Hello everyone! I'm seeking assistance. In my Angular 10 application, I have a component called A. Within this component, there is a <router-outlet> and routes defined in the routes.ts file. My goal is to modify a variable (field) inside comp ...

There was a problem with the module '@angular/material' as it was unable to export a certain member

In creating a custom Angular Material module, I have created a material.module.ts file and imported various Angular Material UI components as shown below: import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/commo ...

Enhancing React with TypeScript and Redux

I am trying to implement React-Redux with TypeScript to dispatch an action. When using mapDispatchToProps(), I am unsure how to define the type of dispatch. Here is my code: This is the component file: import * as React from 'react'; i ...

Effectively enhance constructor by incorporating decorators

Is it possible to properly extend a class constructor with decorators while maintaining the original class name and static attributes and methods? Upon reading the handbook, there is a note that cautions about this scenario: https://www.typescriptlang.or ...

Establish the timeframe for when the dialog box is displayed or closed in Angular version 13

I'm currently using NgDialogAnimationService in Angular 13 and everything is functioning properly. However, I am facing an issue with setting the duration property for when the dialog opens and closes. Unfortunately, I am unable to set this property m ...

Utilizing Angular 2 for Displaying SVG Information

I am facing an issue with my angular application where I need to display product information in a SVG image obtained from a service in JSON format. Despite trying different methods, I have been unsuccessful in rendering the image on the HTML template. Att ...

When attempting to redirect to the home page in Angular 6, the page successfully redirects but fails to load properly

I am new to using angular. Recently, I converted a large project from html/css/php/js to twig/slim, and switched the hosting platform from apache2/sql to s3 buckets/lambda apis. While I have successfully converted smaller projects to angular without any i ...

Switch up text colors on the fly in Angular as you type!

I've been developing a Note Taking application where users can type in red text. Additionally, they should also have the ability to change the color of the remaining text to black while typing. ...

When utilizing Ionic with Angular, it is difficult to access the hardware back button on mobile devices that have buttons located within the display/screen

When trying to access the hardware back button in my app, I encountered an issue where I couldn't produce an alert message to the user before the app closed. After posting a question on Stack Overflow (link of the question) and receiving help from the ...

Adding relationships between models in an Angular/Drupal project using angular2-jsonapi

Why am I getting the error "parseHasMany - Model type for relationship node--emplacement not found" when I try to add a relationship into the findRecord() method? I already have the same error. I don't understand why this is happening. Here's ...

Combining data from various API calls into one cohesive array using RXJS

My RXJS Pipeline is structured as follows: const logs: number[] = [1, 2, 3, 4]; const url = 'http://some-url-here.com'; const pipeline = from(logs).pipe( switchMap(logId => this.callEndpoint(url, logId).pipe(map(response => response. ...