Firestore功能Console.log

时间:2019-12-01 02:18:49

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

我想念什么?尝试使用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
}

感谢任何想法!

1 个答案:

答案 0 :(得分:0)

您未进入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是指什么? ?我认为您那里的概念有误