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

Node.js 使用nodejs从firestore文档中删除字段,node.js,google-cloud-firestore,Node.js,Google Cloud Firestore,我试图从Firestore文档中删除字段,但未成功 我已经搜索了我得到的错误消息,但是我能找到的几页链接指向我已经在做的事情。我相信这件事很简单,但我现在不知所措 const Firestore = require('@google-cloud/firestore'); const admin = require('firebase-admin'); const FieldValue = admin.firestore.FieldValue; const firestore = new Fir

我试图从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)

显然,您不能在云SDK和封装云SDK的Firebase Admin SDK之间混用符号。Firebase导出自己的符号,不能与直接转到Cloud SDK的调用一起使用。呼叫者必须使用其中一个,但不能同时使用两个

编辑:

下面是最后运行的代码,只使用了CloudSDK,没有使用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()
}); 
import * as admin from 'firebase-admin';

const firestoreInstance = admin.firestore();
const fieldValue = admin.firestore.FieldValue;
const docRef = firestoreInstance.collection('XXXXXXX').doc('XXXXXXX');
const batch = firestoreInstance.batch();

...

batch.update(docRef, { fieldToBeDeleted: fieldValue.delete() });

return batch.commit();

我在写批处理的云函数中遇到了类似的问题。最初,我被允许从云SDK中使用docRef=admin.firestore.collection'XXXXXXX'。doc'XXXXXX'以及FieldValue.delete

直到最近,我才开始在代码中出现错误,我不知道我的问题是什么,因为我没有更改代码的这一部分,直到我遇到这个问题和Doug的答案。无论如何,我的解决方案有点不同,但仍然符合Doug的建议,只使用AdminSDK

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()
}); 
import * as admin from 'firebase-admin';

const firestoreInstance = admin.firestore();
const fieldValue = admin.firestore.FieldValue;
const docRef = firestoreInstance.collection('XXXXXXX').doc('XXXXXXX');
const batch = firestoreInstance.batch();

...

batch.update(docRef, { fieldToBeDeleted: fieldValue.delete() });

return batch.commit();

今天我遇到了一个类似的错误,结果是版本不匹配的问题。
我的应用程序有一个正在导入firebase admin的库,但它是旧版本的。FieldValue.delete是从较新版本导入的,并由导入旧版本firebase admin的代码保存。这就是该符号未被识别的原因。

看起来您混合使用了Firebase管理SDK和云SDK。尝试只使用其中一种符号,而不是同时使用两种符号。您使用的是云SDK中的Firestore,但管理SDK中的FieldValue.delete.Doug做到了这一点。谢谢你的帮助。我从顶部删除了2行AdminSDK内容,并将删除行更改为fieldToBeDeleted:Firestore.FieldValue.delete