Cedric
Cedric

Reputation: 990

Deleting maps in a firestore document (cloud functions / Node.js)

I have tried many different ways, but nothing works. All I need is to delete a map (within another map called question_template) in a firestore document, where the name of the map is only known at runtime (given as a parameter languageName to the cloud function).

const modulesDocument = admin.firestore().collection('quiz').doc('modules');

when using template strings, I get this error (I'm using JS btw, not TS):
Property assignment expected.ts(1136)

modulesDocument.update({
    `question_template.${languageName}`: admin.firestore.FieldValue.delete()   
});

then I tried this method, which also didn't work:

const language = `question_template.${languageName}`;

modulesDocument.update({
    //the previously defined 'const language' doesn't get referenced here, why?
    language: admin.firestore.FieldValue.delete()   
});

Upvotes: 0

Views: 112

Answers (2)

Rajitha Udayanga
Rajitha Udayanga

Reputation: 1732

use

modulesDocument.update({
    [`question_template.${languageName}`]: admin.firestore.FieldValue.delete()   
});

or

const language = `question_template.${languageName}`;

modulesDocument.update({
    [language]: admin.firestore.FieldValue.delete()   
});

Upvotes: 1

Doug Stevenson
Doug Stevenson

Reputation: 317487

If you want to use the contents of a string expression as a key in an object definition, put it in square brackets:

modulesDocument.update({
    [language]: admin.firestore.FieldValue.delete()   
});

Upvotes: 1

Related Questions