简体   繁体   中英

Firestore Function Console.log

What am I missing? Trying to use console.log() to debug a Firestore function. The firestore function logs show the error details, however I cannot seem to get console.log() to add debugging message to the Log:

Here is the function Log error:

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)

Here is the function code in 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
}

Thanks for any ideas!

You are not getting into the console.log cause your code is breaking at this line:

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

since it cannot find this , it will try to set this.document.id and crash.

so to your specific question, console.log works as you think it does, just make sure you code reach that log first, you can move the log before the error line and you should get it

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 })

})

The other thing you need to figure out is what does this refers to? 🤔 I think you have an error of concepts there

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM