在电子邮件注册方法的.onCreate触发器中未定义用户displayName

sup*_*p-f 3 ios firebase firebase-authentication firebase-realtime-database

我尝试.onCreate在Firebase云功能中实现firebase云功能codeLab(https://codelabs.developers.google.com/codelabs/firebase-cloud-functions/#7)

exports.addWelcomeMessages = functions.auth.user().onCreate(event => {
  const user = event.data;
  console.log('user: %j', user);
  const fullName = user.displayName;

  return admin.database().ref('messages').push({
    name: 'Firebase Bot',
    text: `${fullName} signed in for the first time! Welcome!`
  });
});
Run Code Online (Sandbox Code Playgroud)

在我的客户端iOS应用程序中,我使用FirebaseUI的电子邮件身份验证流程,要求用户输入他的电子邮件,名称和密码进行注册.用户注册后,.onCreate云功能触发器触发,但user结构不包含displayName数据:

user: {"email":"my@gmail.com","metadata":{"createdAt":"2017-04-05T05:30:05.000Z","lastSignedInAt":"2017-04-05T05:30:05.000Z"},"uid":"wpIPNvf2suQdMumsEyXA5lQatt23"}
Run Code Online (Sandbox Code Playgroud)

注意:注册过程后,iOS应用中当前用户实例中的displayName属性FIRUser正确填充.

我认为FirebaseUI中的某种错误是首先使用电子邮件创建一个空用户,触发.onCreate触发器并且仅在此填充displayName属性之后.是吗?

如果没有,可能是什么原因造成的?

Dev*_*ter 6

这个答案不是Firebase开发人员想要的正确方法,但是因为文档中的代码不起作用,因为在首次创建帐户时不会检索用户displayName属性,所以我想出了一个简单的解决方法.

此代码仅使用此处的管理Node.js SDK:(https://firebase.google.com/docs/auth/admin/manage-users)

这是代码:

const admin = require('firebase-admin'); // This is the required SDK
admin.initializeApp(functions.config().firebase); // SDK initialization is required 

exports.addWelcomeMessages = functions.auth.user().onCreate(event => {
  const user = event.data;
  console.log('user: %j', user);
  const uid = user.uid;

  return admin.auth().getUser(uid)
  .then(function(userRecord){
    // See the UserRecord reference doc for the contents of userRecord.
    console.log("Successfully fetched user data:", userRecord.toJSON());
    const fullName = userRecord.displayName || 'Anonymous';
    return admin.database().ref('messages').push({
      name: 'Firebase Bot',
      text: `${fullName} signed in for the first time! Welcome!`
    });
  })
  .catch(function(error){
    console.log("Error fetching user data:", error);
  });
});
Run Code Online (Sandbox Code Playgroud)

此代码必须对数据库进行额外调用,但至少它解决了onCreate事件数据中不可用的displayName问题.它适用于我自己的测试,但希望有人能够解决实际问题而无需解决方法.

如果这不适合你,请告诉我!干杯!


Kun*_*rma -1

要设置用户显示名称,您首先必须创建用户。

它一开始不会有显示名称属性。

创建用户后,您可以触发修改请求以将显示名称添加到用户。