AngularFire2 (@angular/fire) 如何从可调用函数中获取错误?

Mat*_*w M 6 error-handling typescript angularfire google-cloud-functions angularfire2

我正在尝试使用 Angular Fire 包访问 Angular 环境中可调用函数的错误消息。请看下面的代码:

角度(客户端)

async myCallableFunction(id: string) {
  try {
    const res = await this.afFunctions.httpsCallable('callableFunction')({
      id
    }).toPromise();

    console.log(res);
  } catch (err) {
    console.error('Error is:', err);
  }
}
Run Code Online (Sandbox Code Playgroud)

服务器端(Firebase 功能)

exports.callableFunction = functions.https.onCall((data: {
 id: string
}, context: https.CallableContext) => {
  // throw error for testing only
  throw new https.HttpsError('unknown', 'Test Error Message');
});
Run Code Online (Sandbox Code Playgroud)

控制台记录的错误消息是:

[console.error]:“错误是:”{“代码”:“未知”,“行”:100205,“列”:32,“sourceURL”:“ http://192.168.1.100:8100/vendor.js ” }

如何从 Cloud Firestore 的响应中访问错误消息?

提前致谢。

小智 0

在过去的一个小时里我一直被这个问题困扰,所以我将为那些徘徊在这里的人发布我的答案:

console.error('Error is:', err);
Run Code Online (Sandbox Code Playgroud)

不显示 err 对象的全部内容。如果您想访问 err 对象的 3 个参数中的任何一个,则必须直接访问它们:

  console.error(e.code);
  console.error(e.message);
  console.error(e.details);
Run Code Online (Sandbox Code Playgroud)