首页 > 解决方案 > 如何使用本机反应在我的firestore上的文档上添加多个对象?

问题描述

我有我的收藏,在我的收藏上我有一个数组,在这个数组上我想添加一个多个对象。

使用我的方法,我只能添加一个对象,我想在我的数组上添加不同的对象

这是我的方法:

 async function RegisterTeam () {
        // User is signed in.
        firestore()
    .collection('Users')
    .doc(await AsyncStorage.getItem ('userID'))
    .update({
        "MyTeam.newTeamObj":[{
            Activity:activity,
            City:city,
            Adress:adress,
            text: text,
            members: members,


        Owner:true
        }]
  
        })
            .then(() => {
                console.log('team created !')
                
            
            })         
        }

这是我的模型:

在此处输入图像描述

标签: iosreact-nativegoogle-cloud-firestore

解决方案


您想将更多对象添加到数组中,还是将不同对象添加到 newTeaObj 属性?

.update({ myTeam: firestore.FieldValue.ArrayUnion(data) })

或者

const prevData = [...myTeam]

    .udapte({ myTeam: [...prevData, ...newObject]})

或者您可以使用子集合。


推荐阅读