简体   繁体   中英

TypeError: Cannot read property 'length' of undefined - Ionic and WordPress

I want to connect Ionic with WordPress API Angular. But I get this 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)

Why is this problem?

I don't know the problem becouse in wp.js (provider wp-api-angular) I have this code and obj, is not in the code:

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;
      }
    }
  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM