user-1
(User 1)
December 14, 2022, 5:59pm
1
This topic was automatically generated from Slack. You can find the original thread here .
I am trying to use firestore sdk to delete a field in document . For that I need to pass the field name for update method dynamically which is throwing me an error . It says fieldname is undefined. How do I pass that dynamically in the update method??
user-2
(User 2)
December 14, 2022, 5:59pm
2
you can add field
as a prop in your Node code step. For example as the image below:
user-1
(User 1)
December 14, 2022, 5:59pm
3
I have added that :import admin from ‘firebase-admin’;
import firestore from ‘firebase-admin’;
export default {
name: “DeleteField”,
description: "Delete a field of Firestore document ",
key: “del_field”,
version: “0.0.2”,
type: “action”,
props: {
path: {
type: “string”,
label: “Path”,
description: “A relative Path to the location of document”
},
field:{
type: “string”,
label: “Field”,
description: “Name of the field to delete”
},
firebase_admin_sdk: {
type: “app”,
app: “firebase_admin_sdk”,
}
},
async run() {
const { projectId, clientEmail, privateKey } = this .firebase_admin_sdk.$auth
const formattedPrivateKey = privateKey.replace(/\n/g , ‘\n’)
if (!admin.apps.length) {
admin.initializeApp({
credential: admin.credential.cert({
projectId,
clientEmail,
privateKey: formattedPrivateKey,
})
})
}
const db = admin.firestore();
const record = db.doc(this .path);
**var** fieldname=**this**.field;
record.update(
{
fieldname : firestore.DELETE_FIELD,
}
)
},
}
user-1
(User 1)
December 14, 2022, 5:59pm
4
But fieldname I am unable to pass that in record.update method , it says fieldname is declared but not used
user-2
(User 2)
December 14, 2022, 5:59pm
5
Ah I see, the fieldname
on your code is not the dynamic value. You can try to change the last part your code to
var fieldname=this.field;
record.update(
{
[fieldname] : firestore.DELETE_FIELD,
}
)
user-1
(User 1)
December 14, 2022, 5:59pm
6
Now I am able to pass fieldname dynamically , but the error still exists
user-2
(User 2)
December 14, 2022, 5:59pm
7
could you console log all the values that sent to firestore (.i.e firestore.DELETE_FIELD) to debug which value is undefined?
user-1
(User 1)
December 14, 2022, 5:59pm
8
Working . Using this code const res = await record.update({
[fieldname]: admin.firestore.FieldValue.delete ()
});