0

I am facing this problem with a Promise, I want to return a promise when I select one picture from Gallery using ImagePicker plugin, and then work with the file.

This is my code:

takePictureFromGallery() : any {
        let context = imagepicker.create({
            mode: "single"
        });
        context
            .authorize()
            .then(function() {
                return context.present();
            })
            .then(function(selection) {
                selection.forEach(function(selected) {
                    dialogs.prompt({
                        title: Globals.APP_NAME,
                        message: "Picture description (optional)",
                        okButtonText: "OK",
                        cancelButtonText: "Cancel",
                        inputType: dialogs.inputType.text
                    }).then(r => {
                        if (r.result) {
                            parameters.Headers['Description'] = "";
                            if (r.text != '') parameters.Headers['Description'] = r.text;
                        }
                        let imageSource = new imageSourceModule.ImageSource();
                        imageSource.fromAsset(selected).then((source) => {
                            let savepath = fileSystem.knownFolders.documents().path;
                            let filename = 'img_' + new Date().getTime() + '.' + "jpg";
                            let filepath = fileSystem.path.join(savepath, filename);

                            var picSaved = source.saveToFile(filepath, "jpg");

                            return new Promise((resolve, reject) => {
                                if (picSaved) resolve(filepath);
                                else reject();
                            });

                        })
                    })
                })
            }).catch(function (e) {
                this._feedback.error({message: "You must allow " + Globals.APP_NAME + " access to your Gallery"});
            });

    }

This method belongs to a class called PictureUploader. Outside this class I have this code:

let pu = new PictureUploader();
pu.takePictureFromGallery()
    .then(s => {
        console.log(s);
})

I get undefined is not an object when I try to run the .then() method.

Any help? Thanks!

relez
  • 750
  • 2
  • 13
  • 28

2 Answers2

2

You need to place your promise statement in the very beginning like so:

takePictureFromGallery() : any {
    return new Promise((resolve, reject) => {
        let context = imagepicker.create({
            mode: "single"
        });
        context
            .authorize()
            .then(function() {
                return context.present();
            })
            .then(function(selection) {
                selection.forEach(function(selected) {
                    dialogs.prompt({
                        title: Globals.APP_NAME,
                        message: "Picture description (optional)",
                        okButtonText: "OK",
                        cancelButtonText: "Cancel",
                        inputType: dialogs.inputType.text
                    }).then(r => {
                        if (r.result) {
                            parameters.Headers['Description'] = "";
                            if (r.text != '') parameters.Headers['Description'] = r.text;
                        }
                        let imageSource = new imageSourceModule.ImageSource();
                        imageSource.fromAsset(selected).then((source) => {
                            let savepath = fileSystem.knownFolders.documents().path;
                            let filename = 'img_' + new Date().getTime() + '.' + "jpg";
                            let filepath = fileSystem.path.join(savepath, filename);

                            var picSaved = source.saveToFile(filepath, "jpg");

                            if (picSaved) resolve(filepath);
                            else reject();

                        })
                    })
                })
            }).catch(function (e) {
                this._feedback.error({message: "You must allow " + Globals.APP_NAME + " access to your Gallery"});
            });

    });
}

And you need to take care of rejecting the promise also in the catch block (not shown in the example)

Sebastian Hildebrandt
  • 2,661
  • 1
  • 14
  • 20
1

I'm recommend you to return an observable .

you can read about it here Promise vs Observable

implements in the same why just like this

takePictureFromGallery() : observable<any> {
return new observable((o) => {
    let context = imagepicker.create({
        mode: "single"
    });
    context
        .authorize()
        .then(function() {
            return context.present();
        })
        .then(function(selection) {
            selection.forEach(function(selected) {
                dialogs.prompt({
                    title: Globals.APP_NAME,
                    message: "Picture description (optional)",
                    okButtonText: "OK",
                    cancelButtonText: "Cancel",
                    inputType: dialogs.inputType.text
                }).then(r => {
                    if (r.result) {
                        parameters.Headers['Description'] = "";
                        if (r.text != '') parameters.Headers['Description'] = r.text;
                    }
                    let imageSource = new imageSourceModule.ImageSource();
                    imageSource.fromAsset(selected).then((source) => {
                        let savepath = fileSystem.knownFolders.documents().path;
                        let filename = 'img_' + new Date().getTime() + '.' + "jpg";
                        let filepath = fileSystem.path.join(savepath, filename);

                        var picSaved = source.saveToFile(filepath, "jpg");

                        if (picSaved){
                           o.next(filepath);
                           o.complete();
                          }   
                        else o.error();

                    })
                })
            })
        }).catch(function (e) {
            this._feedback.error({message: "You must allow " + Globals.APP_NAME + " access to your Gallery"});
        });

});
}
Matan Shushan
  • 1,204
  • 1
  • 10
  • 25