[英]Ionic - Uncaught (in promise): TypeError: Cannot read property 'then' of undefined
I got this error during runtime of my Ionic Application.我在 Ionic 应用程序运行时收到此错误。 I was trying to implement the Ionic "Email Composer" library to send emails from the client side, please reference this link to learn about the code that I am following;
我正在尝试实现 Ionic “Email Composer” 库以从客户端发送电子邮件,请参考此链接以了解我正在遵循的代码; https://ionicframework.com/docs/native/email-composer .
https://ionicframework.com/docs/native/email-composer 。 The flow of the error was when I was testing the application I hit a button that triggers the "LoadingDialog" function this function triggers the "SendEmail" function which is where the error occurs.
错误的流程是当我测试应用程序时,我点击了一个按钮,触发了“LoadingDialog”function 这个 function 触发了“SendEmail”ZC1C425268E68385D1AB5074F14A 发生错误的地方。 I am not sure how to fix this error, but I think it has to do with the syntax of a promise that I don't understand yet.
我不确定如何修复此错误,但我认为它与我还不理解的 promise 的语法有关。 Thank you for any help you can provide!
感谢您提供任何帮助!
this.emailComposer.isAvailable().then((available: boolean) =>{ //<<<<<<<< THIS IS LINE 221 IN home.page.ts file and it appears in the error
this.emailComposer.isAvailable().then((available: boolean) =>{ //<<<<<<< THIS IS LINE 221 IN home.page.ts 文件,它出现在错误中
this.SendEmail();
this.SendEmail(); //<<<<<<<< THIS IS LINE 109 IN home.page.ts file
//<<<<<<<< 这是 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)
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");
}
});
}
}
I'm not sure weather you found the source of the problem yet.我不确定你是否找到了问题的根源。 But the problem is related to your env, you need to test in your emulator / phone in order to avoid this exception .
但问题与您的环境有关,您需要在模拟器/手机中进行测试以避免此异常。
This error occurred because I did not run the app in the emulator or on a Android phone, since the Cordova library only runs with Mobile OS's like Android OS, so the this.emailComposer.isAvailable() was undefined instead of returning a promise. This error occurred because I did not run the app in the emulator or on a Android phone, since the Cordova library only runs with Mobile OS's like Android OS, so the this.emailComposer.isAvailable() was undefined instead of returning a promise. The minimum that you need is:
您需要的最低要求是:
if (this.platform.is('cordova')){ //run your email composer code here }
if (this.platform.is('cordova')){ //在这里运行你的 email 作曲家代码 }
This code worked for me after I ran the code on a android phone:在我在 android 手机上运行代码后,此代码对我有用:
if (this.platform.is('cordova')){//use cordova for android,iOS,Windows-Mobile,etc
this.emailComposer.isAvailable().then((available:
boolean) =>{
//CODE HERE
});
}else{
console.log("Platform is not cordova/mobile","You are running in browser, this.emailComposer.isAvailable() is undefined.");
}
Here is the source code for the email composer: https://ionicframework.com/docs/native/email-composer这是 email 作曲家的源代码: https://ionicframework.com/docs/native/email-composer
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.