Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete a field in a Firestore doc from a cloud function

how can I delete a field in a document from a cloud firestore function ?

I'm using transactions, so I tried this approach :

    const { FieldValue } = require('@google-cloud/firestore');
    ...   
    ..
    transaction.update(docRef, { fieldToRemove: FieldValue.delete() });

but I'm getting this error :

Transaction failure: Error: Update() requires either a single JavaScript object or an alternating list of field/value pairs that can be followed by an optional precondition. Argument "dataOrField" is not a valid Document. Couldn't serialize object of type "DeleteTransform". Firestore doesn't support JavaScript objects with custom prototypes (i.e. objects that were created via the 'new' operator).

Thanks !

like image 390
user3574857 Avatar asked Aug 16 '18 15:08

user3574857


1 Answers

You're right to use FieldValue.delete() as per the docs, but need to import it from 'firebase-admin' instead:

const admin = require('firebase-admin');
const FieldValue = admin.firestore.FieldValue;
transaction.update(docRef, { fieldToRemove: FieldValue.delete() });
like image 177
embirico Avatar answered Oct 25 '22 17:10

embirico