Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot read property 'length' of undefined #34

Open
davidperezgar opened this issue Nov 26, 2017 · 5 comments
Open

TypeError: Cannot read property 'length' of undefined #34

davidperezgar opened this issue Nov 26, 2017 · 5 comments

Comments

@davidperezgar
Copy link

Why do 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)

@davidperezgar
Copy link
Author

Hello, I see in my dependencies this error:
UNMET PEER DEPENDENCY @angular/core@4.4.6
How can i solve it?

@shprink
Copy link
Collaborator

shprink commented Nov 30, 2017

Hello,

what version of angular do you use?
can you share the code you use when getting this error?

@davidperezgar
Copy link
Author

It was 5.0.0. But I've changed to 4.4.6, but the error still remains...

@shprink
Copy link
Collaborator

shprink commented Dec 4, 2017

can you share the code you use when getting this error?

@davidperezgar
Copy link
Author

davidperezgar commented Dec 4, 2017

import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map';
import { Observable } from 'rxjs/Observable';
import { WpApiPosts, WpApiMedia, WpApiUsers } from 'wp-api-angular';
 
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;
      }
    }
  }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants