Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
175 views
in Technique[技术] by (71.8m points)

javascript - Firestore功能Console.log(Firestore Function Console.log)

What am I missing?

(我想念什么?)

Trying to use console.log() to debug a Firestore function.

(尝试使用console.log()调试Firestore函数。)

The firestore function logs show the error details, however I cannot seem to get console.log() to add debugging message to the Log:

(firestore函数日志显示错误详细信息,但是我似乎无法使console.log()向日志添加调试消息:)

Here is the function Log error:

(这是函数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)

Here is the function code in index.js:

(这是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!

(感谢您的任何想法!)

  ask by tomN translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

(您没有进入console.log因为您的代码在此行中断:)

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

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

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

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

(因此,对于您的特定问题, 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 })

})

The other thing you need to figure out is what does this refers to?

(你需要弄清楚的另一件事是什么呢this是指?)

?? I think you have an error of concepts there

(??我认为您那里的概念有误)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...