【发布时间】:2018-12-15 14:11:33
【问题描述】:
我正在尝试部署一个 Firebase 云功能,每次在事件节点下添加新事件时,它都会向我的 algolia 搜索引擎推送一些内容。我遵循了这里的教程,它似乎详细说明了如何做大部分事情。
https://www.youtube.com/watch?v=Njtbo3YUdH4
但是,当我运行我的项目时,我遇到了最奇怪的错误 解析函数触发器时出错。
TypeError: Cannot read property 'config' of undefined
我过去在尝试部署函数时从未见过此错误。我不确定是否发生了变化,但到目前为止我真的找不到在 firebase 文档或 stackoverflow 上修复此错误的位置。
我正在运行节点版本 v8.11.3,此外我很确定我的 algolia 配置正确,因为当我运行时
$ firebase functions:config:get
我明白了
{
"algolia": {
"appid": "xxxxxxx",
"adminkey": "xxxxxxxxxx"
}
}
唯一可能的就是我的代码。如果有人可以帮助我修复它,我将在下面包含它,我将不胜感激。
//similar to import functions in swift
const functions = require('firebase-functions');
// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
//exports acts as a global variable that you can init other properties on
//on
// exports.helloWorld = functions.https.onRequest((request, response) => {
// response.send("Hello from Firebase!");
// });
const algoliasearch = require('algoliasearch');
const algolia = algoliasearch(functions.config().algolia.appid,functions.algolia.config().adminKey)
exports.updateIndex = functions.database.ref('/events/{eventKey}').onWrite(event =>{
const index = algolia.initIndex('events');
const eventKey = event.params.eventKey
const data = event.data.val();
if(!data){
return index.deleteObject(eventKey,(err) =>{
if (err) throw err
console.log('Event deleted from algolia index',eventKey)
})
}
data['objectID'] = eventKey
return index.saveObject(data,(err,content) =>{
if (err) throw err
console.log('Event updated in algolia index',data.objectID)
})
});
【问题讨论】:
-
您在什么时候看到该错误?请描述您的工作流程。
-
当我尝试部署函数时
标签: node.js firebase google-cloud-functions algolia firebase-cli