Firebase v3 createUserWithEmailAndPassword .then Promise [英] Firebase v3 createUserWithEmailAndPassword .then Promise

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

问题描述

Firebase v3 参考指南表明createUserWithEmailAndPassword(email, password) 返回包含非 null firebase.Userfirebase.Promise.

Firebase v3 Reference Guide indicates that createUserWithEmailAndPassword(email, password) returns firebase.Promise containing non-null firebase.User.

Firebase v3 使用基于密码的帐户指南使用 Firebase 进行身份验证显示以下示例

The Firebase v3 Authenticate with Firebase using Password-Based Accounts Guide shows the following example

firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(error) {
  // Handle Errors here.
  var errorCode = error.code;
  var errorMessage = error.message;
  // ...
});

我的问题是,我想触发一个函数,将用户添加到我的实时数据库中的 users 节点.我想包括我从网站注册表中收集的用户显示名称.所以,似乎我想做一个 .then 并且如果 createUserWithEmailAndPassword 方法成功,我想触发一个函数,将新用户(带有显示名称)写入我的实时数据库 users 节点.

My question is, I want to fire a function which adds the user to my users node in my Realtime Database. I want to include the users display name that I collected from the sites registration form. So, it seems like I want to do a .then and if the createUserWithEmailAndPassword method was successful I want to fire a function that writes the new user (with display name) to my Realtime Database users node.

如何将示例脚本修改为 .then 样式?

How do I modify the example script to a .then style?

推荐答案

以下是 then() 的正确使用和 createUserWithEmailAndPassword 的错误处理:

Here is the correct use of then() and error handling with createUserWithEmailAndPassword:

firebase.auth().createUserWithEmailAndPassword(email, password).then(function(user) {
    var user = firebase.auth().currentUser;
    logUser(user); // Optional
}, function(error) {
    // Handle Errors here.
    var errorCode = error.code;
    var errorMessage = error.message;
});

function logUser(user) {
    var ref = firebase.database().ref("users");
    var obj = {
        "user": user,
        ...
    };
    ref.push(obj); // or however you wish to update the node
}

我有另一个例子这里.

这篇关于Firebase v3 createUserWithEmailAndPassword .then Promise的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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