简体   繁体   English

错误:未捕获(承诺):[object Object]

[英]Error: Uncaught (in promise): [object Object]

@Injectable()
class MyErrorHandler implements ErrorHandler {

  handleError(error) {
    // exception occured in some service class method.
    console.log('Error in MyErrorhandler - %s', error);
      if(error == 'Something went wrong'){
       //do this.
     }else{
      //do this thing.
    }
  }
}

When in some class' method throws an exception then, the MyErrorHandler class prints the error caught as Error in MyErrorhandler - Error: Uncaught (in promise): [object Object] Error: Something went wrong.当在某个类中的方法抛出异常时,MyErrorHandler 类会将捕获的Error in MyErrorhandler - Error: Uncaught (in promise): [object Object] Error: Something went wrong.打印为Error in MyErrorhandler - Error: Uncaught (in promise): [object Object] Error: Something went wrong.

Question1 : Why does the error displays as Error: Uncaught (in promise): [object Object] ?问题 1 :为什么错误显示为Error: Uncaught (in promise): [object Object]

Question2 : Due to the above message it will always read the else part even in the case of if(error == 'Something went wrong') of the code in any condition.How can I resolve this?问题 2 :由于上述消息,即使在任何情况下代码的if(error == 'Something went wrong')的情况下,它也将始终读取 else 部分。我该如何解决这个问题?

Try adding尝试添加

if(error.message == 'Something went wrong'){
}

instead of only error.而不仅仅是错误。 As error is an object.因为错误是一个对象。

is necesary your check the response in yours promise...有必要检查你的承诺中的回应......

you have to capture resolve and reject ...你必须捕捉决心拒绝......

generally this happens because the value of reject not is capture通常发生这种情况是因为拒绝的值不是被捕获

for example例如

CREATE PROMISE...创造承诺...

promise= () => {
    return new Promise((resolve, reject) => {
      if (condition) {
         resolve(true);
      }else{
        reject(false);
      }
    });
}

And here capture ALL posible response in promise..在这里捕获承诺中的所有可能的响应..

this.services.promise().then(
    (data) => {
       here capture your resolve
       },
    (err) => {
       here capture your resolve
    }
);

OR

 this.services.promise()
   .then((res) => {
       console.log('response in resolve = ', )});
    ).catch((err)=> {
      console.log('response in reject = ', err)
    })

actualmente estoy trabajando de esta manera.实际情况 estoy trabajando de esta manera。

`import { Injectable, ErrorHandler, Injector } from '@angular/core'; `import { Injectable, ErrorHandler, Injector } from '@angular/core'; import { AlertService } from './_alert.service';从 './_alert.service' 导入 { AlertService };

@Injectable({ providedIn: 'root' }) export class ErroresService implements ErrorHandler { @Injectable({ providedIn: 'root' }) 导出类 ErroresService 实现 ErrorHandler {

constructor(private sv_alert:AlertService) { }构造函数(私有 sv_alert:AlertService){}

handleError(err:any) { const message = err.message ? handleError(err:any) { const message = err.message ? err.message: err.message.toString(); err.message: err.message.toString(); this.sv_alert.AlertaMensajeError( ${ message } ); this.sv_alert.AlertaMensajeError( ${ message } ); } } ` } }`

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:未捕获(承诺):[object Boolean] - Error: Uncaught (in promise): [object Boolean] 错误:错误错误:未捕获(承诺):[对象布尔] - Error : ERROR Error: Uncaught (in promise): [object Boolean] 吞下的消息:错误:未捕获(承诺):[对象未定义] - Swallowed message : Error: Uncaught (in promise): [object Undefined] 未被捕获(承诺):错误:找不到“ [对象对象]”的NgModule元数据 - Uncaught (in promise): Error: No NgModule metadata found for '[object Object]' ng-recaptcha ERR:错误:“未捕获(承诺):[对象空]” - ng-recaptcha ERR: Error: "Uncaught (in promise): [object Null]" ionic3-错误错误:未捕获(承诺):错误:InvalidPipeArgument:管道“ AsyncPipe”的“ [object Object]” - ionic3 - ERROR Error: Uncaught (in promise): Error: InvalidPipeArgument: '[object Object]' for pipe 'AsyncPipe' Wasm: Uncaught (in promise) TypeError: Import #0 module="env" error: module is not an object or function Promise.then (async) (anonymous) @ (index):9 - Wasm: Uncaught (in promise) TypeError: Import #0 module="env" error: module is not an object or function Promise.then (async) (anonymous) @ (index):9 地图组件偶尔会抛出错误:Uncaught (in promise), "H.map.DataModel#add (Argument #0 [object Object])" - Map component occasionally throws an error: Uncaught (in promise), "H.map.DataModel#add (Argument #0 [object Object])" Angular Uncaught对象错误 - Angular Uncaught object error p5 Uncaught (in promise) 在另一个 class 中实例化 object 时出错 - p5 Uncaught (in promise) Error when instantiating an object inside another class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM