为什么 Firestore Cloud Function return 请求响应错误?

Why does Firestore Cloud Function return a bad request response?

我正在 using custom claims for authorization 上关注 firebase 的 youtube 频道上的教程。在尝试调用云函数时,我不断收到来自 firebase 服务器的 'bad request: Invalid argument' 响应。该函数甚至从未被调用。我猜 data 结构不正确(参数无效),但不确定原因。有人可以解释一下吗?

云函数:

exports.addAdmin = functions.https.onCall( (data: any, context: any) => {
  const email = data.email;
  return grantAdminRole(email).then(() => {
    return{
      result: 'Admin role has been assigned successfully'
    }
  }).catch( err => {
    return{
      error: err
    }
  })
})

async function grantAdminRole(email: string): Promise<void> {
  const user = await admin.auth().getUserByEmail(email);
  if( user.customClaims && user.customClaims.admin === true ) {
    return;
  } else {
    return admin.auth().setCustomUserClaims(user.uid, {
      admin: true
    })
  }
};

客户:

endpoint = 'https://[MY_FUNCTION_URL]';
userEmail = 'test@test.com';

constructor( private http: HttpClient ) {}

grantAdminRole() {
  this.http.post(this.endpoint, this.userEmail ).subscribe( res => {
    console.log(res);
  });
}

您的 server-side 实现了 callable Cloud Functions,如下所示:

functions.https.onCall

但是您的客户然后尝试将其作为 HTTP Cloud Function 调用它:

this.http.post(this.endpoint, ...

虽然可调用函数是在 HTTP 函数之上构建的,但它们不能以相同的方式调用。你应该 use the client-side functions SDK to call the callable Cloud Function, or implement the wire protocol.