从云端功能删除Firestore文档中的字段

时间:2018-08-16 15:17:58

标签: firebase google-cloud-firestore google-cloud-functions

如何从Cloud Firestore函数中删除文档中的字段?

我正在使用事务,所以我尝试了这种方法:

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

但我遇到此错误:

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).

谢谢!

1 个答案:

答案 0 :(得分:4)

您可以按照the docs使用FieldValue.delete(),但需要从“ firebase-admin”导入它:

const admin = require('firebase-admin');
const FieldValue = admin.firestore.FieldValue;
transaction.update(docRef, { fieldToRemove: FieldValue.delete() });