首页 文章

TypeError:无法读取未定义的属性'length' - Ionic和WordPress

提问于
浏览
0

我想将Ionic与WordPress API Angular连接起来 . 但我得到这个错误:

TypeError:无法读取未定义的属性“length”

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)

这是为什么这个问题?

1 回答

  • 0

    我不知道wp.js中的问题(提供者wp-api-angular)我有这个代码和obj,不在代码中:

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

相关问题