gpt4 book ai didi

javascript - 使用nodejs从firestore文档中删除字段

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

我尝试从 Firestore 文档中删除字段,但没有成功。

我已经搜索了收到的错误消息,但我可以找到的几页链接指向我已经在做的事情。我确信这很简单,但我现在不知所措。

const Firestore = require('@google-cloud/firestore');
const admin = require('firebase-admin');
const FieldValue = admin.firestore.FieldValue;

const firestore = new Firestore({
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
});
const settings = {timestampsInSnapshots: true};
firestore.settings(settings);


var updateDoc = firestore.collection('XXXXXXXX').doc('XXXXXXX').update({
fieldToBeDeleted: FieldValue.delete()
});

我希望删除该字段,但我收到此错误消息:

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).
at WriteBatch.update (XXXXXXXXXX\node_modules\@google-cloud\firestore\build\src\write-batch.js:359:23)
at DocumentReference.update (XXXXXXXXXX\node_modules\@google-cloud\firestore\build\src\reference.js:387:14)
at Object.<anonymous> (XXXXXXXXXX)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:191:16)

最佳答案

显然,您不能在 Cloud SDK 和 Firebase Admin SDK(包装了 Cloud SDK)之间混合使用符号。 Firebase 导出自己的符号,这些符号不能用于直接转至 Cloud SDK 的调用。调用者必须使用其中之一,但不能同时使用两者。

编辑:

以下是最终运行的代码,仅使用 Cloud SDK,没有使用 Firebase Admin SDK。

const Firestore = require('@google-cloud/firestore');

const firestore = new Firestore({
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
});
const settings = {timestampsInSnapshots: true};
firestore.settings(settings);


var updateDoc = firestore.collection('XXXXXXXX').doc('XXXXXXX').update({
fieldToBeDeleted: Firestore.FieldValue.delete()
});

关于javascript - 使用nodejs从firestore文档中删除字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57062699/

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