gpt4 book ai didi

javascript - 无法使用函数 :Shell 将新文档添加到 Firestore 中的集合

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

我正在尝试使用 Firebase shell 将文档添加到 Firestore 中的子集合:

firebase 函数:shell

运行它为我们提供了一个可以运行查询和代码的外​​壳。出于某种奇怪的原因,我似乎无法添加文档到集合中。

这是我在 shell 中运行的脚本:

  await db.collection('balances')
.doc(docId)
.collection('history')
.add({x: 'a'});

这非常简单,但 Firebase 似乎不喜欢将该对象传递到 add 函数中。

我能够到达 Google Firestore validate.js 文件中一切都失败的确切行。

function extractBaseClassName(value) {
let constructorName = 'Object';
while (Object.getPrototypeOf(value) !== Object.prototype) {
value = Object.getPrototypeOf(value); // <===== This returns null instead of an Object for some reason
constructorName = value.constructor.name;
}
return constructorName;
}

这是确切的错误消息堆栈:

(node:90760) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'constructor' of null
at extractBaseClassName (/Users/Moose/Development/crypto-accumulator/functions/node_modules/@google-cloud/firestore/build/src/validate.js:35:33)
at Object.customObjectMessage (/Users/Moose/Development/crypto-accumulator/functions/node_modules/@google-cloud/firestore/build/src/validate.js:55:26)
at Object.validateDocumentData (/Users/Moose/Development/crypto-accumulator/functions/node_modules/@google-cloud/firestore/build/src/write-batch.js:608:36)
at CollectionReference.add (/Users/Moose/Development/crypto-accumulator/functions/node_modules/@google-cloud/firestore/build/src/reference.js:1765:23)
at runB (repl:43:6)
at process._tickCallback (internal/process/next_tick.js:68:7)

更新:预感...

我认为问题可能是 Object.getPrototypeOf(value) !== Object.prototype 计算结果为 true,而它应该是 false code> 因为 {x: 'a'} 是一个对象

最佳答案

看来对象没有问题。我尝试在我的 Firestore 中重现它并使用以下命令:

let  docId = 'test_document3'
async function f1() {
await db.collection('test_collection').doc(docId).collection('inside-collection').add({ x: 'c'})
}
f1()

文档已添加,没有问题。您也可以尝试同样的方法。

关于javascript - 无法使用函数 :Shell 将新文档添加到 Firestore 中的集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59013268/

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