首页 > 解决方案 > 如何在数组上添加一个对象而不用firestore删除其余对象?

问题描述

我正在尝试在我的数组上添加一个对象。我的模型已经存在他是这样的:

function createUser () {
  
      // User is signed in.
      firestore()
  .collection('Users')
  .doc(user.uid)
  .set({
      NickName: nickName,
      Age: age,
      City:city,
      Mail:mail,
      Picture:'',
      MyTeam:[{
          Activity:"",
          City:"",
          adress:"",
          text:"",
          Members:[{
            Picture:"",
              Mail:"",
              Pseudo:""
          }],
      Owner:true
      }]

在“我的团队”对象上,我希望能够添加几个团队(如数组),但是当我尝试设置时,我的所有模型都被删除了。

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


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

你知道我该怎么做吗?我正在反应原生 ios

标签: iosreact-nativegoogle-cloud-firestore

解决方案


您可以使用.update()代替.set(). 为了不覆盖整个对象,您应该使用这样的点符号

firestore()
.collection('Users')
.doc(await AsyncStorage.getItem ('userID'))
.update({
  "MyTeam.newTeamObj": {newObj}
})

https://firebase.google.com/docs/firestore/manage-data/add-data#update_fields_in_nested_objects


推荐阅读