【问题标题】:firebase JS not able to Add firstname lastname after authfirebase JS无法在身份验证后添加名字姓氏
【发布时间】:2018-05-11 16:37:44
【问题描述】:

我正在使用 react-native redux 应用程序。这是我新创建的 firebase 数据库。

一切正常,用户也可以登录和注册。但是我在注册后有这个小代码我正在尝试.set({ firstname, lastname }) firstname, lastname,它没有反映在数据库中。 (github 仓库:https://github.com/steelx/react-native-redux-app

    export const signUpUser = ({ firstname = "", lastname = "", email, password }) => (dispatch) => {
    dispatch({ type: SIGN_UP_REQUEST });

    firebase.auth().createUserWithEmailAndPassword(email, password)
        .then((user) => {

            // const currentUser = firebase.auth().currentUser;
            // currentUser.updateProfile({
            //     displayName: `${firstname} ${lastname}`,
            //     THIS WORKS
            // })

            firebase.database().ref('users/' + user.uid)
            // THIS DOES NOT WORK
                .set({ "firstname": 'AJINKYA', "lastname": 'AJINKYA' })
                .then(() => {
                    dispatch({ type: SIGN_UP_SUCCESS, payload: user });
                    Actions.home();
                });
        })
        .catch((error) => { dispatch({ type: SIGN_UP_FAILURE, payload: authFailMessage(error.code) }); });
};

Firebase 规则

{
  "rules": {
    "users": {
      "$uid": {
        ".read": "auth != null",
        ".write": "auth != null && auth.uid == $uid"
      }
    }
  }
}

【问题讨论】:

  • 这里的firstnamelastname 是什么?
  • “测试”、“测试”String
  • 我发现我只能设置displayName。这很好用 ==> firebase.auth().currentUser.updateProfile({displayName: ${firstname} ${lastname}}) 但我的问题仍然存在,如果我想在数据库中添加条目怎么办。
  • 酷。当您将这些值硬编码到set 时,您能否看到是否可以重现它,所以.set({ firstname: "test", lastname: "test" })。如果仍然重现它,我们可以排除变量的范围问题。
  • 嗯...这可能是权限问题。您能否将catch 添加到set 并记录任何错误。所以.set({ "firstname": 'AJINKYA', "lastname": 'AJINKYA' }).catch((error) => { console.error(error); }).then(...

标签: javascript firebase firebase-realtime-database firebase-authentication


【解决方案1】:

抱歉,我在参考文献中将uid 传递为undefined。最初在发布此问题之前,规则被设置为锁定。

我的规则也是错误的。后来我将规则更新为 => 作为快速修复,我添加了这些规则。现在一切正常。

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null"
  }
}

【讨论】:

    猜你喜欢
    • 2018-03-17
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    • 2014-04-13
    • 1970-01-01
    • 1970-01-01
    • 2020-09-14
    • 2016-02-22
    相关资源
    最近更新 更多