离子-未捕获(承诺):TypeError:无法读取未定义的属性“ then”

时间:2019-11-16 18:50:23

标签: angular ionic-framework promise angular-promise

我在离子应用程序运行期间遇到此错误。我正在尝试实现Ionic“ Email Composer”库以从客户端发送电子邮件,请参考此链接以了解我所关注的代码; [https://ionicframework.com/docs/native/email-composer][1]。 错误的流程是当我测试应用程序时,我按下了一个触发“ LoadingDialog”功能的按钮,该功能触发了发生错误的“ SendEmail”功能。我不确定如何解决此错误,但我认为这与我尚不了解的Promise的语法有关。感谢您提供的任何帮助!

错误中出现的代码行

  

this.emailComposer.isAvailable()。then((available:boolean)=> {   // <<<<<<<<这是home.page.ts文件中的221行,它显示在错误中

     

this.SendEmail(); // <<<<<<<<这是home.page.ts文件中的109行

错误

ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'then' of undefined   TypeError: Cannot read property 'then' of undefined
     at HomePage.SendEmail (home.page.ts:221)
     at HomePage.<anonymous> (home.page.ts:109)
     at Generator.next (<anonymous>)
     at fulfilled (tslib.es6.js:70)
     at ZoneDelegate.invoke (zone-evergreen.js:359)
     at Object.onInvoke (core.js:34201)
     at ZoneDelegate.invoke (zone-evergreen.js:358)
     at Zone.run (zone-evergreen.js:124)
     at zone-evergreen.js:855
     at ZoneDelegate.invokeTask (zone-evergreen.js:391)
     at resolvePromise (zone-evergreen.js:797)
     at zone-evergreen.js:707
     at fulfilled (tslib.es6.js:70)
     at ZoneDelegate.invoke (zone-evergreen.js:359)
     at Object.onInvoke (core.js:34201)
     at ZoneDelegate.invoke (zone-evergreen.js:358)
     at Zone.run (zone-evergreen.js:124)
     at zone-evergreen.js:855
     at ZoneDelegate.invokeTask (zone-evergreen.js:391)
     at Object.onInvokeTask (core.js:34182)

代码(home.page.ts)

            import { EmailComposer } from '@ionic-native/email-composer/ngx';

            export class HomePage {

            constructor(private http: Http, public loadingController: LoadingController,
                    public alertController: AlertController, 
                    private emailComposer: EmailComposer) {
           //constructor
            }
        async LoadingDialog(httpformdata) {//loading circle symbol
            const loading = await this.loadingController.create({
                message: 'Please wait...',
                duration: null
            });
            this.loading = loading;
            this.loading.present();
            console.log('ion dialog created.');
            /***  SendEmail  ***/
            this.SendEmail(); //<<<<<<<< THIS IS LINE 109 IN home.page.ts file
            /***  GET REQUEST  ***/
            this.GET_request(httpformdata);
            /***  POST REQUEST  ***/
            //this.POST_request(httpformdata);
            console.log('request made in async.');

            //this.loading.dismiss();
            //console.log('ion dialog dismissed.');

            const { role, data } = await loading.onDidDismiss();

            console.log('Loading dismissed!');
        }
            async RegularAlert(the_header: String, the_message: String) {//customized alert() dialog
            const alert = await this.alertController.create({
                header: '' + the_header,
                message: '' + the_message,
                buttons: ['OK']
            });
            await alert.present();
            return alert.getAttribute("header");
        }

        SendEmail(){
                this.emailComposer.isAvailable().then((available: boolean) =>{  //<<<<<<<< THIS IS LINE 221 IN home.page.ts file
                    if(available) {
                    //Now we know we can send
                    this.RegularAlert("testing the isAvailable().then", "available is true");//print a debug message to test functionality of   

                    let email = {
                        to:'max@mustermann.de',
                        cc: 'erika@mustermann.de',
                        bcc: [],
                        attachments: [],
                        subject: 'Cordova Icons',
                        body: 'How are you? Nice greetings from Leipzig',
                        isHtml: false
                      }

                      // Send a text message using default options
                      this.emailComposer.open(email);
                    }
                    else{
                        this.RegularAlert("testing the isAvailable().then", "available is false");
                    }
                });

        } 

    }

2 个答案:

答案 0 :(得分:1)

我不确定您是否找到问题的根源。但是问题与您的环境有关,您需要在模拟器/手机中进行测试,以避免出现此异常

答案 1 :(得分:0)

发生此错误是因为我没有在模拟器或Android手机中运行该应用程序,因为Cordova库仅在移动操作系统(如Android OS)上运行。您所需的最低要求是:

如果(this.platform.is('cordova')){//在此处运行电子邮件编写器代码}

在我在Android手机上运行代码后,此代码对我有用:

if (this.platform.is('cordova')){//use this for android,iOS,Windows-Mobile,etc){
            try {
                this.emailComposer.isAvailable().then((available: boolean) =>{
                    if(available) {
                        //Now we know we can send
                        let email = {
                            to: 'test1@gmail.com',
                            cc: 'test2@gmail.com',
                            bcc: [],
                            attachments: [],
                            subject: 'Cordova Icons',
                            body: 'How are you? Nice greetings from Leipzig',
                        isHtml: false
                        }//email object close

                    //Send a text message using default options
                    this.emailComposer.open(email);//opens user prompt to use third party email app with the email message above.
                    }//if close 
                    else{
                        console.log("Rare Error inside of the promise returned by the emailComposer.isAvailable()", "promise error");//TEST PRINT
                    }
                });
            } catch(e){
                  //catches the "this.emailComposer.isAvailable()" variable if the email composer plugin is not available on the installed phone   
            }
        }//if this.platform.is close
        else{
            console.log("Platform is not cordova/mobile","You are running in browser, this.emailComposer.isAvailable() is undefined.");//TEST PRINT
        }//else close

这是电子邮件编辑器的源代码:https://ionicframework.com/docs/native/email-composer