0

Add Function:

exports.newCampaign = functions.https.onCall((data, context) => {
    // get adventurer information
    const campaignName = data.campaignName;
    const timestamp = data.timestamp;

    const uid = context.auth.token.uid || null;

    // create a new document for the campaign
    var query = admin.firestore().collection(`Adventurers/${uid}/campaigns`).add({
        locations: [],
        name: campaignName
    })
    .then((docRef) => {
        admin.firestore().collection('Adventurers').doc(uid).update({
            [`campaignList.${docRef.id}`]: {
                id: docRef.id,
                name: campaignName,
                timestamp: timestamp
            }
        });

        return {
            id: docRef.id
        };
    })
    .catch((error) => {
        return {
            error: "Error adding document: " + error
        };
    });

    return query;
});

When I call this, the Adventurers.uid.campaigns collection gets a new document with campaignId, and the Adventurers.uid.campaignList gets an attribute called campaignId. Works as expected

Delete Function:

exports.deleteCampaign = functions.https.onCall((data, context) => {
    // get adventurer information
    const campaignId = data.campaignId;
    const uid = context.auth.token.uid || null;

    // delete the document for the campaign
    var query = admin.firestore().collection(`Adventurers/${uid}/campaigns`).doc(campaignId).delete()
    .then((docRef) => {
        // delete campaign  from the campaign list
        admin.firestore().collection('Adventurers').doc(uid).update({
            [`campaignList.${campaignId}`]: Firebase.firestore.FieldValue.delete()
        });

        return {};
    })
    .catch((error) => {
        return {
            error: "Error removing document: " + error
        };
    });

    return query;
});

When I call this, the document in the campaigns collection gets deleted, but the field in the campaignList does not get deleted. Does not work as expected. Since the document got deleted successfully, then I know that the function is getting the correct campaignId. I'm trying to delete in the exact same way as I added, so why isn't it working?

Emma
  • 51
  • 1
  • 7

1 Answers1

0

Similar post here

Since I'm running in a cloud function, I need to call admin.firestore.FieldValue.delete() instead of Firebase.firestore.FieldValue.delete() in my delete function.

Emma
  • 51
  • 1
  • 7