gpt4 book ai didi

firebase - 从云函数删除 Firestore 文档中的字段

转载 作者:行者123 更新时间:2023-12-02 21:53:52 25 4
gpt4 key购买 nike

如何从云 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).

谢谢!

最佳答案

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

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

关于firebase - 从云函数删除 Firestore 文档中的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51880416/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com