【问题标题】:Unable to add new documents to a collection in Firestore using Functions:Shell无法使用 Functions:Shell 将新文档添加到 Firestore 中的集合
【发布时间】:2019-11-23 23:32:04
【问题描述】:

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

firebase functions:shell

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

这是我在 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 因为 {x: 'a'} 是一个对象

【问题讨论】:

    标签: javascript firebase google-cloud-platform google-cloud-firestore


    【解决方案1】:

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

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

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

    【讨论】:

    • 很有趣,所以我将代码移到了一个 firebase 函数中并以这种方式调用它,它运行良好。似乎是firebase shell的一个问题。我可能需要向他们提出一个错误
    • 如果我理解正确 await 只能在异步函数内部调用,至少我不知道其他方式......
    猜你喜欢
    • 1970-01-01
    • 2019-04-28
    • 2020-01-05
    • 1970-01-01
    • 1970-01-01
    • 2019-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多