Firebase Cloud Function-null.createName onCreate [英] Firebase Cloud Function - null user.displayName onCreate

查看:35
本文介绍了Firebase Cloud Function-null.createName onCreate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个函数,该函数将在创建用户时发送欢迎电子邮件.我遵循了教程,其中说可以使用 user.displayName 访问新创建的用户的 displayName ,尽管它会一直为我返回 null .我意识到,发生这种情况的可能原因(如果我在这里错了,请纠正我)是注册和设置用户的displayName发生在客户端的两个单独步骤中,因此,当触发 onCreate 时, user.displayName 自然会为null.这是我的客户端代码,仅供参考:

I'm trying to write a function that will send a welcome email on user creation. I followed along to this tutorial, which says that one can access the newly created user's displayName with user.displayName, though it keeps returning null for me. I realized that the probable reason that this happens (correct me if I'm wrong here) is that registration and setting the user's displayName happens in two separate steps client-side and therefore when onCreate is triggered, user.displayName will naturally be null. Here's my client-side code, for reference:

fb.auth().createUserWithEmailAndPassword(payload.email, payload.password).then(user => {
  return user.user.updateProfile({ displayName: payload.name, });
}).catch(/* ... */);

我正在寻找的是在 user.updateProfile 上触发的云功能.我研究了 auth.user().onOperation 函数(找到了

What I'm looking for is a cloud function that gets triggered on user.updateProfile. I've looked into the auth.user().onOperation function (found here), but when I try to deploy this function to firebase, I get the error Error: Functions did not deploy properly. (helpful, ikr), which I guess has something to do with the onOperation function being private (correct me if I'm wrong).

有什么办法可以做我想做的事吗?如果是这样,怎么办?或者,是否可以在 createUserWithEmailAndPassword 上设置 displayName ,以便我可以继续使用 onCreate 函数?

Is there any way to do what I'm trying to do? If so, how? Alternatively, is there a way to set the displayName on createUserWithEmailAndPassword, so that I can keep using the onCreate function?

这是我当前的 onCreate 代码:

exports.sendWelcomeEmail = functions.auth.user().onCreate(user => {
  console.log('name:', user.displayName);
});

这是我对 onOperation 函数的尝试:

exports.sendWelcomeEmail = functions.auth.user().onOperation(user => {
  console.log('user:', user);
}, 'updateProfile');

推荐答案

如果我在哪里,我将不使用firebase auth作为用户的个人资料.使用 users 的集合也许是一个更好的主意,您可以在其中访问更新触发器.

If I where you, I wouldnt use firebase auth for user's profile. Maybe is a better idea to use a collection of users, where you have access to update triggers.

通常,我要做的是在有新的auth用户时触发一个触发器,在 users 集合中创建一个用户.通过这种设计,每当有人更新个人资料时,您都可以触发一个触发器.

Normally, what I do is to have a trigger when there is new auth user, create a user in the userscollection. With this design you could have a trigger everytime someone updates it's profile.

exports.onCreateAuthUser = functions.auth.user().onCreate(user => {
  firestore.collection('users').doc(user.uid).set({
     displayName: user.displayName,
     email: user.email,
     // any other properties 
  })

  //do other stuff
});

exports.onUpdateUser = functions
  .firestore.document('users/{id}')
  .onUpdate((change, context) => {
     // do stuff when user's profile gets updated
}

希望有帮助:)

这篇关于Firebase Cloud Function-null.createName onCreate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆