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

library.forEach is not a function #178

Open
liuboren0617 opened this issue Sep 24, 2019 · 2 comments
Open

library.forEach is not a function #178

liuboren0617 opened this issue Sep 24, 2019 · 2 comments

Comments

@liuboren0617
Copy link

liuboren0617 commented Sep 24, 2019

The example on the official website has an error

http://www.ionic.wang/native_doc-index-id-231.html

image

this.photoLibrary.requestAuthorization().then(() => {
  this.photoLibrary.getLibrary().subscribe({
    next: library => {
    library.forEach(libraryItem => {
                          console.log(libraryItem.id);          // ID of the photo
                          console.log(libraryItem.photoURL);    // Cross-platform access to photo
                          console.log(libraryItem.thumbnailURL); //  Cross-platform access to thumbnail
                          console.log(libraryItem.fileName);
                          console.log(libraryItem.width);
                          console.log(libraryItem.height);
                          console.log(libraryItem.creationDate);
                          console.log(libraryItem.latitude);
                          console.log(libraryItem.longitude);
                          console.log(libraryItem.albumIds);    // array of ids of appropriate AlbumItem, only of includeAlbumsData was used
                      });
                },
    error: err => { console.log('could not get photos'); },
    complete: () => { console.log('done getting photos'); }
  });
})
.catch(err => console.log('permissions weren\'t granted'));
@FutureArchitect-takeda1874

I have the same problem.
It seems that getLibrary() returns object like {isLastChunk: true, library: [...]}.

@athielking
Copy link

Modifying the example code to this fixes the error for me:

this.photo.requestAuthorization({read: true}).then( () => {
      this.photo.getLibrary().subscribe({
        next: (lib: any) => {
          lib.library.forEach(function(libraryItem) {
            console.log(libraryItem.id);          // ID of the photo
            console.log(libraryItem.photoURL);    // Cross-platform access to photo
            console.log(libraryItem.thumbnailURL);// Cross-platform access to thumbnail
            console.log(libraryItem.fileName);
            console.log(libraryItem.width);
            console.log(libraryItem.height);
            console.log(libraryItem.creationDate);
            console.log(libraryItem.latitude);
            console.log(libraryItem.longitude);
            console.log(libraryItem.albumIds);    // array of ids of appropriate AlbumItem, only of includeAlbumsData was used
          });
        },
        error: err => { console.log('could not get photos'); },
        complete: () => { console.log('done getting photos'); }
      });
    })

Additionally you could create a new interface which matches the object that getLibrary() returns and cast it to that. The any cast is just quick and dirty.

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

3 participants