flamelink / flamelink-js-sdk

🦊 Official Flamelink JavaScript SDK for both the Firebase Realtime database and Cloud Firestore
https://flamelink.github.io/flamelink-js-sdk
MIT License
43 stars 5 forks source link

How do I update an array of references? #191

Closed wahaj-47 closed 2 years ago

wahaj-47 commented 2 years ago
app.content.get({
      schemaKey: "events",
      fields: [
        "id",
        "eventName",
        "date",
        "image",
        "registeredPlayers",
        "course",
      ],
      populate: [
        {
          field: "image",
          fields: ["url"],
        },
        {
          field: "course",
          fields: ["courseName", "courseAddress", "coursePar"],
        },
        {
          field: "registeredPlayers",
        },
      ],
    });

The field registeredPlayers is a tree so I get an array of references but because I am populating it, I get the document data. Now I want to update this array and push another reference into it. But I don't have the references so if I try:

app.content.update({
        schemaKey: "events",
        entryId: item.id,
        data: {
          registeredPlayers: [...item.registeredPlayers, userDocument.ref],
        },
      }); 

Then I end up with an array containing a document object and a document reference.

gitdubz commented 2 years ago

Hi @wahaj-47

Assuming registeredPlayers is relational to Flamelink User (fl_users)

const registeredPlayersRefs = item.registeredPlayers.map({ uid } => firebaseApp.firestore().collection('fl_users').doc(uid));

app.content.update({
        schemaKey: "events",
        entryId: item.id,
        data: {
          registeredPlayers: [...registeredPlayersRefs, userDocument.ref],
        },
      }); 

note firebaseApp is not the flamelink app instance but the raw firebaseApp you instantiated

Does this help?

wahaj-47 commented 2 years ago

Yeah I came up with the same thing. I was just wondering if maybe we could do it without having to map over the array. Thanks.