gpt4 book ai didi

javascript - 火存储 : Couldn't Serialize object of type 'ArrayUnionTransform'

转载 作者:行者123 更新时间:2023-12-03 00:29:41 29 4
gpt4 key购买 nike

我正在尝试使用 Firestore 文档示例更新数组中的元素:

https://firebase.google.com/docs/firestore/manage-data/add-data#update_elements_in_an_array

var admin = require('firebase-admin');

var washingtonRef = firestore.collection('cities').doc('DC');

var arrUnion = washingtonRef.update({
regions: admin.firestore.FieldValue.arrayUnion('greater_virginia')
});

但是,当我这样做时,我收到以下错误:

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 "ArrayUnionTransform" (found in field
regions). Firestore doesn't support JavaScript objects with custom
prototypes (i.e. objects that were created via the "new" operator).

最佳答案

确保您使用正确的 SDK。

这不起作用的原因是因为我对 firestore.collection('cities').doc('DC'); 的引用实际上来自这里:

const Firestore = require('@google-cloud/firestore'); // this is the wrong SDK.

const firestore = new Firestore({
projectId: 'my-project'
keyFilename: fbKeyFile
});

正如 README.md 上所述这是错误的:

Applications that use Google's Server SDKs should not be used in end-user environments, such as on phones or on publicly hosted websites. If you are developing a Web or Node.js application that accesses Cloud Firestore on behalf of end users, use the firebase Client SDK.

为了让它工作,我简单地切换了以下内容:

firestore.collection('cities').doc('DC');
// to
FirebaseAdmin.firestore().collection('cities').doc('DC');

关于javascript - 火存储 : Couldn't Serialize object of type 'ArrayUnionTransform' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53925361/

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