How To Fix "Unknown error status: Error: The uid must be a non-empty string with at most 128 characters." in Firebase Functions

How To Fix "Unknown error status: Error: The uid must be a non-empty string with at most 128 characters." in Firebase Functions

我正在尝试通过将数据从 UI 传递到我

的可调用函数来在我的 firebase 应用程序中创建一个用户
  1. 使用电子邮件密码创建一个用户帐户,然后
  2. 添加显示名称,然后在用户集合中创建配置文件,然后
  3. 发送用户确认电子邮件,但我收到错误 Unknown error status: Error: The uid must be a non-empty string with at most 128 characters. at new HttpsError (/srv/node_modules/firebase-functions/lib/providers/https.js:102:19) at admin.auth.createUser.then.then.then.catch.error (/srv/index.js:41:12) at <anonymous>
const db = admin.firestore();
exports.createUser = functions.https.onCall((data,context)=>{
  return admin.auth().createUser({
    email: data.email,
    password: data.password,
    displayName: data.displayName,
  }).then(user =>{
    return db.doc('users/'+user.uid).set({
      email: data.email,
      displayName:data.displayName,
      type:data.type,
      organization:data.organization
    });
  })
  .then(user=>{
    let uid = user.uid;
    if (data.type === "admin"){
      return admin.auth().setCustomUserClaims(uid,{
        isAdmin: true,
      })
    }else{
      return admin.auth().setCustomUserClaims(uid,{
        isAdmin: false,
      })
    }
  })
  .then(user =>{ 
    return user.sendEmailVerification();
  })
  .catch(error =>{
     new functions.https.HttpsError(error);
  });
})

这是我在 React JS 前端的代码

    let createUser = functions.httpsCallable('createUser')
    createUser({
      email: this.state.email,
      password: this.state.password,
      displayName:this.state.name,
      type:this.state.type,
      organization:this.state.organization
    })
    .then(result => {
        console.log(result)
    })
    .catch(error => {
        console.log(error)
    })

当你

return db.doc('users/'+user.uid).set({
  email: ....});
})
.then(user => { // here, user is undefined})

user 的值(即 实现值 ,或者换句话说,您传递给第一个回调函数的参数 您传递给 then 方法) 未定义 因为 set() 方法 returns a "non-null Promise containing void".

需要将前面then()uid的值保存在一个变量中,如下代码所示


另请注意,通过这样做,

  .then(user =>{ 
    return user.sendEmailVerification();
  })

首先你会遇到与上面相同的问题(user 的值未定义),但是,此外,在 Admin SDK 中没有 sendEmailVerification() 方法,这是一个客户端方法JavaScript SDK。

您可以使用 Admin SDK 的 generateEmailVerificationLink() 方法并通过电子邮件(从 Cloud Function)将 link 发送给用户,例如通过 Sendgrid。

const db = admin.firestore();
exports.createUser = functions.https.onCall((data,context)=>{

  let userUid;

  return admin.auth().createUser({
    email: data.email,
    password: data.password,
    displayName: data.displayName,
  }).then(user =>{
    userUid = user.uid;
    return db.doc('users/'+userUid).set({
      email: data.email,
      displayName:data.displayName,
      type:data.type,
      organization:data.organization
    });
  })
  .then(()=>{
    if (data.type === "admin"){
      return admin.auth().setCustomUserClaims(userUid,{
        isAdmin: true,
      })
    }else{
      return admin.auth().setCustomUserClaims(userUid,{
        isAdmin: false,
      })
    }
  })
  .then(() =>{ 
    //You may use the generateEmailVerificationLink() method, see
    //https://firebase.google.com/docs/reference/admin/node/admin.auth.Auth#generateEmailVerificationLink

    const actionCodeSettings = ....
    return admin.auth()
       .generateEmailVerificationLink(data.email, actionCodeSettings)
  })
  .then(link => {
     //The link was successfully generated.
     //Send an email to the user through an email service

     //See https://github.com/firebase/functions-samples/tree/master/email-confirmation

     //or 
  })
  .catch(error =>{
       throw new functions.https.HttpsError('unknown', error.message);
  });
})