【发布时间】: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