在push()

时间:2019-02-01 21:46:24

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

我有以下代码:

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))
    }
}

...并得到以下结果:

Result in firebase

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

  

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

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

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

1 个答案:

答案 0 :(得分:1)

当你传递一个目的是火力地堡,该属性的值可以是一个值或空值。不能不确定它们,

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
                    })