簡體   English   中英

如何在不使用 firestore 刪除 rest 的情況下在陣列上添加 object?

[英]How to add an object on an array wthout to delete the rest with firestore?

我正在嘗試在我的陣列上添加一個 object。 我的 model 已經存在 他是這樣的:

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

在 object“我的團隊”中,我希望能夠添加幾個團隊(如數組),但是當我嘗試設置時,我的所有模型都被刪除了。

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

您可以使用.update()而不是.set() 並且不覆蓋整個 object 你應該使用這樣的點符號

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

我認為最好的方法是在這里使用子集合而不是 arrays。在這種情況下,您將擁有:

users (collection)
    |
    ---------- teams (sub-collection) 
                 |
                 ------------ members (sub-sub-collection)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM