【问题标题】:Why does Firestore Cloud Function return a bad request response?为什么 Firestore Cloud Function 返回错误的请求响应?
【发布时间】:2020-08-04 21:52:45
【问题描述】:

我正在关注 using custom claims for authorization 上 firebase 的 youtube 频道上的教程。在尝试调用云函数时,我不断收到来自 firebase 服务器的“错误请求:无效参数”响应。该函数甚至从未被调用。我猜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);
  });
}

【问题讨论】:

    标签: javascript firebase google-cloud-firestore google-cloud-functions angularfire2


    【解决方案1】:

    您的服务器端实现了callable Cloud Functions,如下所示:

    functions.https.onCall
    

    但您的客户随后会尝试将其作为HTTP Cloud Function 调用:

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

    虽然可调用函数是在 HTTP 函数之上构建的,但它们不能以相同的方式调用。您应该use the client-side functions SDK 调用可调用的云函数,或者implement the wire protocol

    【讨论】:

      猜你喜欢
      • 2022-01-19
      • 2021-12-06
      • 1970-01-01
      • 2019-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-17
      相关资源
      最近更新 更多