Ionic - Uncaught (in promise): TypeError: Cannot read property 'then' of undefined
Ionic - Uncaught (in promise): TypeError: Cannot read property 'then' of undefined
我在 Ionic 应用程序运行时遇到此错误。我正在尝试实现 Ionic“Email Composer”库以从客户端发送电子邮件,请参考此 link 以了解我正在关注的代码; https://ionicframework.com/docs/native/email-composer。
错误的流程是当我测试应用程序时,我点击了一个触发“LoadingDialog”函数的按钮,这个函数触发了“SendEmail”函数,这是错误发生的地方。我不确定如何修复此错误,但我认为它与我尚不理解的承诺语法有关。感谢您提供的任何帮助!
错误中出现的代码行
this.emailComposer.isAvailable().then((available: boolean) =>{
//<<<<<<<< THIS IS LINE 221 IN home.page.ts file and it appears in the error
this.SendEmail(); //<<<<<<<< THIS IS LINE 109 IN home.page.ts file
错误
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");
}
});
}
}
我不确定您是否找到了问题的根源。但是这个问题与你的环境有关,你需要在你的模拟器/phone中测试以避免这个异常。
发生此错误是因为我没有 运行 模拟器中或 Android phone 上的应用程序,因为 Cordova 库仅 运行 具有移动 OS 就像 Android OS,所以 this.emailComposer.isAvailable() 是未定义的,而不是返回一个承诺。您至少需要:
if (this.platform.is('cordova')){
//run your email composer code here
}
在我 运行 android phone:
上的代码后,此代码对我有用
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.");
}
这是电子邮件编辑器的源代码:https://ionicframework.com/docs/native/email-composer
我在 Ionic 应用程序运行时遇到此错误。我正在尝试实现 Ionic“Email Composer”库以从客户端发送电子邮件,请参考此 link 以了解我正在关注的代码; https://ionicframework.com/docs/native/email-composer。 错误的流程是当我测试应用程序时,我点击了一个触发“LoadingDialog”函数的按钮,这个函数触发了“SendEmail”函数,这是错误发生的地方。我不确定如何修复此错误,但我认为它与我尚不理解的承诺语法有关。感谢您提供的任何帮助!
错误中出现的代码行
this.emailComposer.isAvailable().then((available: boolean) =>{ //<<<<<<<< THIS IS LINE 221 IN home.page.ts file and it appears in the error
this.SendEmail(); //<<<<<<<< THIS IS LINE 109 IN home.page.ts file
错误
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");
}
});
}
}
我不确定您是否找到了问题的根源。但是这个问题与你的环境有关,你需要在你的模拟器/phone中测试以避免这个异常。
发生此错误是因为我没有 运行 模拟器中或 Android phone 上的应用程序,因为 Cordova 库仅 运行 具有移动 OS 就像 Android OS,所以 this.emailComposer.isAvailable() 是未定义的,而不是返回一个承诺。您至少需要:
if (this.platform.is('cordova')){ //run your email composer code here }
在我 运行 android phone:
上的代码后,此代码对我有用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.");
}
这是电子邮件编辑器的源代码:https://ionicframework.com/docs/native/email-composer