【问题标题】:Firestore Function Console.logFirestore 函数 Console.log
【发布时间】:2019-12-01 02:18:49
【问题描述】:

我错过了什么?尝试使用 console.log() 来调试 Firestore 函数。 firestore 函数日志显示错误详细信息,但我似乎无法让 console.log() 将调试消息添加到日志:

这里是函数日志错误:

TypeError: Cannot read property 'document' of undefined
      at exports.updateIndex.functions.firestore.document.onCreate.event (/srv/index.js:13:36)
      at cloudFunction (/srv/node_modules/firebase-functions/lib/cloud-functions.js:131:23)
      at /worker/worker.js:825:24
      at <anonymous>
      at process._tickDomainCallback (internal/process/next_tick.js:229:7)

这里是index.js中的函数代码:

const functions = require('firebase-functions');

const admin = require('firebase-admin');
admin.initializeApp();

exports.updateIndex = functions.firestore
.document('Documents/{Name}')
.onCreate(event => {

   const documentId = event.params.document.id;
    const id = this.document ? this.document.id : '';
    console.log("Update Index for Doc", document);

    const document = event.after.data();

    console.log("docId",documentId,"id",id);

    const searchableIndex = newFunction(document)

    const indexedDocument = { ...document, searchableIndex }

    const db = admin.firestore()

    return db.collection('Documents').doc(document.id).set(indexedDocument, { merge: true })

})

function newFunction(document) {
    return createIndex(document.Name);
}

function createIndex(document) {
    const arr = Name.toLowerCase().split('');
    const searchableIndex = {}
console.log("Creating Index");
    let prevKey = '';

    for (const char of arr) {
        const key = prevKey + char;
        searchableIndex[key] = true
        prevKey = key
    }
    console.log("Create docId",documentId,"id",id);
    return searchableIndex
}

感谢您的任何想法!

【问题讨论】:

  • 我看不出错误与您显示的代码有什么关系。该代码中的任何地方都没有名为document 的属性。
  • Doug,你是对的,我已经包含了完整的功能。我的问题是如何让 console.log() 消息显示在 Firestore 日志中。谢谢。

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


【解决方案1】:

您没有进入console.log,因为您的代码在这一行中断:

const id = this.document ? this.document.id : '';

由于找不到this,它会尝试设置this.document.id并崩溃。

所以对于您的具体问题,console.log 按您的想法工作,只需确保您的代码首先到达该日志,您可以将日志移到错误行之前,您应该得到它

exports.updateIndex = functions.firestore
.document('Documents/{Name}')
.onCreate(event => {

   const documentId = event.params.document.id;

    /*********
     HERE IT WILL WORK
    ********/
    console.log(documentId) // Cool!

    const id = this.document ? this.document.id : ''; // ERROR LINE
    console.log("Update Index for Doc", document);

    const document = event.after.data();

    console.log("docId",documentId,"id",id); // WONT WORK

    const searchableIndex = newFunction(document)

    const indexedDocument = { ...document, searchableIndex }

    const db = admin.firestore()

    return db.collection('Documents').doc(document.id).set(indexedDocument, { merge: true })

})

您需要弄清楚的另一件事是this 指的是什么? ? 我认为你的概念有错误

【讨论】:

  • 是的,我以前没有用过这个,所以现在我知道该怎么做了。谢谢!
猜你喜欢
  • 2021-06-26
  • 1970-01-01
  • 2014-12-23
  • 2017-05-22
  • 2015-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多