【问题标题】:Error Deploying Firebase function部署 Firebase 功能时出错
【发布时间】: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


【解决方案1】:

看这一行(我加了一个回车):

const algolia = algoliasearch(functions.config().algolia.appid,
    functions.algolia.config().adminKey)

这看起来是正确的:

functions.config().algolia.appid

这不是:

functions.algolia.config().adminKey

看起来你是想说这个:

functions.config().algolia.adminKey

【讨论】:

  • *捶额头
  • 哦,等一下,我现在有另一个问题
  • 函数加载错误:无法加载文件 index.js 中的代码。您是否在 package.json 依赖项中列出了所有必需的模块?详细的堆栈跟踪:错误:找不到模块“algoliasearch”
  • 我必须 npm 安装吗?
  • 如果您遇到的问题与您在问题中提出的问题不同,请提出一个全新的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-19
  • 2019-03-06
  • 2021-09-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多