【问题标题】:Problem inserting array of child node in push ()在 push() 中插入子节点数组的问题
【发布时间】:2019-06-26 12:09:54
【问题描述】:

我有以下代码:

export const createCharacter = (username, name, sex, vocation, achievementPoints, world) => {
    return (dispath) => {
        firebase.auth().signInWithEmailAndPassword('dev7@gmail.com', '123123')
            .then(value => {
                firebase.database().ref('/sales/')
                    .push({
                        owner: username,
                        character: [
                            {
                                name: name,
                                sex: sex,
                                vocation: vocation,
                                achievementPoints: achievementPoints,
                                world: world
                            }
                        ],
                        price: 21.12
                    })
                    .then(() => {

                    })
            })
            .catch(error => alert(error))
    }
}

...有了它,我得到了以下结果:

当我第一次尝试执行 action 方法时,我发现了这个错误:

错误:Reference.push 失败:第一个参数在属性 sales.character.0.name 中包含未定义

但是当我第二次不刷新执行时,推送成功了。

我的问题是,我应该如何推送才能不生成此 UID?

【问题讨论】:

  • 请在您调用createCharacter的位置以及调用它返回的函数的位置添加代码。

标签: javascript firebase react-native firebase-realtime-database ecmascript-6


【解决方案1】:

当您将对象传递给 Firebase 时,属性的值可以是值或 null。它们不能是未定义的,

firebase.database().ref('/sales/')
                    .push({
                        owner: username || null,
                        character: [
                            {
                                name: name || null,
                                sex: sex || null,
                                vocation: vocation || null,
                                achievementPoints: achievementPoints || null,
                                world: world || null
                            }
                        ],
                        price: 21.12
                    })

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-19
    • 1970-01-01
    • 2019-09-06
    • 2021-07-02
    • 2021-02-25
    相关资源
    最近更新 更多