【问题标题】:firebase set not accepting nested dictionaryfirebase 集不接受嵌套字典
【发布时间】:2021-03-25 10:01:42
【问题描述】:

Firebase 设置功能不适用于嵌套字典

const [appInitialized, setAppInitialized] = useState(false)

// Where FireBase is name of function that initializes firebase

FireBase().then(() => {
    setAppInitialized(true)
})

const [user, loading, error] = useAuthState(firebase.auth());


useEffect(() => {
    if (appInitialized) {
        SingingInAnonymously()
    }
}, [appInitialized, loading])


function SingingInAnonymously() {
    if (!loading) {
        if (!user && !loading) {
            firebase.auth().signInAnonymously()
                .then((response) => {


                    const reference = "Users/" + response.user.uid
                    console.log("RUNNING TO ADD USER", reference)
                    firebase.database().ref(reference).set(
                        {
                            CurrentAvatar: "https://firebasestorage.googleapis.com/v0/b/just-an-app-96ed6.appspot.com/o/Avatars%2FRad.png?alt=media&token=dae7e13b-ac3a-4fd9-a5e5-8342e84dc13e",
                            UnlockedAvatars: ["PushID1"]
                        }
                    ).then((e) => console.log("success")).catch((e)=>console.log(e))
                    setAppInitialized(false)
                }).catch((e) => {
            })
        }
    }

}

我也试过

 UnlockedAvatars: { "0" : "PushID1" }

如果我将“set”更改为“push”,它会起作用,否则它不起作用,不会给出任何错误,也不会进入 then 语句。

如果我删除嵌套字典 UnlockedAvatars: ["PushID1"] / UnlockedAvatars: {"0": "PushID1"} 它可以工作

如果我更改对常量字符串的引用,它仍然有效

【问题讨论】:

    标签: javascript reactjs firebase


    【解决方案1】:

    我已经尝试过了,它确实有效。我创建了一个简单的反应应用程序,它能够成功更新数据库。

    import React from 'react'
    import ReactDOM from 'react-dom'
    import firebase from "firebase/app";
    import 'firebase/database'
    const firebaseConfig = {
      // ...
    }
    
    firebase.initializeApp(firebaseConfig);
    var database = firebase.database()
    const App = () => {
      React.useEffect(() => {
        let reference = 'cities'
        database.ref(reference).set(
          {
              CurrentAvatar: "wer",
              UnlockedAvatars: {"0": "PushID1"},
              extraAvatars: ["PushID1"]
          }
      ).then(() => console.log("success")).catch((e)=>console.log(e))
      }, [])
      return (
        <div>App</div>
      )
    }
    
    
    let container = document.getElementById('app');
    
    ReactDOM.render(<App />, container)
    

    [![显示数据库输出的图像][1]][1] [1]:https://i.stack.imgur.com/Unyo2.png

    【讨论】:

    • 这很奇怪,它可以正常工作,但在我的用例中它不起作用
    • 请再次检查我已经用我的完整代码编辑了帖子
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-02
    • 2012-09-14
    • 2018-01-29
    • 2021-12-20
    • 2012-01-29
    • 2018-05-31
    • 2013-09-19
    相关资源
    最近更新 更多