How to update a dynamic field name in Firebase Firestore?
You were very close, in order to compute the field name, wrap it in square brackets:
await firestore()
.collection('Users')
.doc('ABC')
.update({
[this.state.documentName]: url,
});
The update()
method also accepts arguments in key-value pairs:
await firestore()
.collection('Users')
.doc('ABC')
.update(this.state.documentName, url);
Note: You should make sure to employ appropriate security rules to prevent a user from promoting themselves to an admin if that information is also contained in their user document.