terikon / cordova-plugin-photo-library

Maintainer needed. Please contact if you're using this library in your project
MIT License
149 stars 295 forks source link

library.forEach is not a function #178

Open liuboren0617 opened 4 years ago

liuboren0617 commented 4 years ago

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 commented 4 years ago

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

athielking commented 3 years ago

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.