angular - 在 Angular 中未收到来自 API 的错误响应
问题描述
我在 Angular 中调用 API 并尝试获取响应正文,但它只是显示error message is [object Object]
我试过只是做console.log(error)
, error.text()
, JSON.parse(error._body).errors
, error.error
,error.message
但error.json()
没有奏效。
这是我的代码,非常感谢一些帮助:
doImport = (dataObj) => {
for(let i = 0; i<this.uploadedFilesCount; i++) {
const filename = this.uploadedFileNames[i];
this.disableDeployCTA = true;
const params:string = filename + "/" + this.testStartTime + "/" + this.userNtId + "/" + this.userTeamID + "/" + this.userTeam;
var res = '';
this.httpService.importUML(params).subscribe((response: any) => {
this.toastmessage.success('Successfully created test #'+i);
this.toastmessage.clear();
},
(error) => {
console.log("error message is "+error.message);
if(error.status == 409) {
if(res.indexOf("static data")!=-1) {
this.toastmessage.error(filename + ": That static data exists already!");
}
else {
this.toastmessage.error(filename + ": That test case exists already! Please change test case name.");
}
}
else if(error.status == 400) {
this.toastmessage.error(filename + ": Incorrectly formatted UML. Please fix and try again.");
}
});
}
this.resetAxiForm();
this.disableImport = true;
}
我想得到的错误响应:
解决方案
这是一个很常见的错误console.log
,这里发生的事情是你收到的错误实际上是一个object
,你用那个+
标志做的就是连接string
一个object
肯定会显示的[object Object]
,你需要做的是使用逗号,
而不是+
里面console.log()
。
替换console.log("error message is "+error.message);
为console.log("error message is ", error.message);
希望这有帮助。
推荐阅读
- python - 将行附加到 CSV 文件
- node.js - 在插入 Node.js 时获取已连接 USB 驱动器的挂载路径
- swift - 如何从 UserDefaults 向数组添加值?
- c# - 来自 LINQ Select 的复杂 IEnumerable 结果
- javascript - 如何使用延迟函数以相反的顺序显示圆圈(使用 JSON 数据创建)?
- android - 片段内带有缓存的 Webview
- c# - “IEntityType”不包含“关系”的定义
- python - 使用牛顿拉夫森方法求根
- r - 应用函数 R 中的 if-else 语句
- arrays - 如何在 Tiled 中使用 userData 进行碰撞?(LigGDX)