【问题标题】:How do I run a firebase function locally in one line in the cmd?如何在 cmd 的一行中本地运行 firebase 函数?
【发布时间】:2020-07-30 08:39:29
【问题描述】:

我的操作系统是 Windows 10。
我有一个计划好的 firebase 函数,我想在本地运行:

functions = require('firebase-functions')
admin = require('firebase-admin')
exports.myFunction = functions.pubsub.schedule('every 30 minutes').onRun((context) => {
    console.log('Hello World!')
    return null
})

为了在本地运行这个函数,我运行了两个命令(答案来自here):

firebase functions:shell
myFunction()

我可以在一行代码而不是两行代码中执行此命令吗?

【问题讨论】:

  • 你的主机操作系统是什么?
  • 我的主机操作系统是Windows 10

标签: firebase google-cloud-firestore google-cloud-functions


【解决方案1】:

从调度逻辑中分离出函数逻辑,然后直接用node调用函数:

function.js

const functions = require('firebase-functions')
const admin = require('firebase-admin')
const foo = require('./foo')
exports.myFunction = functions.pubsub.schedule('every 30 minutes').onRun(foo)

foo.js

module.exports = (context) => {
  console.log('Hello World!')
  return null
}

在一行中调用节点:

node -e 'require("./foo")()'

注意:您可能需要在 foo.js 中手动提供配置/凭据,具体取决于您访问的 Firebase 资源,因为当您直接调用它时并没有在云环境中运行它。

【讨论】:

    猜你喜欢
    • 2014-02-05
    • 2021-10-28
    • 2021-04-09
    • 1970-01-01
    • 2021-10-09
    • 2021-05-18
    • 2019-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多