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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46542784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 06:45:30  来源:igfitidea点击:

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

javascriptangularerror-handling

提问by Aditya

@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.

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 部分。我该如何解决这个问题?

回答by Kishori

Try adding

尝试添加

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

instead of only error. As error is an object.

而不仅仅是错误。因为错误是一个对象。

回答by Ruben Palavecino

actualmente estoy trabajando de esta manera.

实际情况 estoy trabajando de esta manera。

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

`import { Injectable, ErrorHandler, Injector } from '@angular/core'; 从 './_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 ? err.message: err.message.toString(); this.sv_alert.AlertaMensajeError(${ message }); } } `

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

回答by Daniel Lopez

is necesary your check the response in yours promise...

有必要检查你的承诺中的回应......

you have to capture resolveand reject...

你必须捕捉决心拒绝......

generally this happens because the value of rejectnot 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
    }
);