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

Ada*_*dam 2 javascript node.js firebase google-cloud-platform google-cloud-firestore

我尝试从 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()
}); 
Run Code Online (Sandbox Code Playgroud)

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

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)
Run Code Online (Sandbox Code Playgroud)

Dou*_*son 5

显然,您不能在 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()
}); 
Run Code Online (Sandbox Code Playgroud)