【问题标题】:Why does running firebase emulators also execute one of my functions?为什么运行 firebase 模拟器也会执行我的一项功能?
【发布时间】:2021-01-11 02:29:28
【问题描述】:

我运行firebase emulators:start --only functions,firestore 并得到以下输出:

$ firebase emulators:start  --only functions,firestore
i  emulators: Starting emulators: functions, firestore
⚠  functions: The following emulators are not running, calls to these services from the Functions emulator will affect production: database, hosting, pubsub
✔  functions: Using node@10 from host.
⚠  functions: Your GOOGLE_APPLICATION_CREDENTIALS environment variable points to /Users/user/development/project/app/serviceKey.json. Non-emulated services will access production using these credentials. Be careful!
i  firestore: Firestore Emulator logging to firestore-debug.log
i  ui: Emulator UI logging to ui-debug.log
i  functions: Watching "/Users/user/development/project/app/functions" for Cloud Functions...

>  TEST TEST 123

✔  functions[test-func1]: http function initialized (http://localhost:5001/project-9999d/us-east1/test-func1).
✔  functions[test-func2]: http function initialized (http://localhost:5001/project-9999d/us-east1/test-func2).
✔  functions[test-func3]: firestore function initialized.
✔  functions[test1-func1]: http function initialized (http://localhost:5001/project-9999d/us-east1/test1-func1).
✔  functions[test1-func2]: http function initialized (http://localhost:5001/project-9999d/us-east1/test1-func2).
✔  functions[test3-func1]: http function initialized (http://localhost:5001/project-9999d/us-east1/test3-func1).
✔  functions[test3-func2]: http function initialized (http://localhost:5001/project-9999d/us-east1/test3-func2).
✔  functions[test4]: http function initialized (http://localhost:5001/project-9999d/us-east1/test4).
i  functions[test5-func1]: function ignored because the pubsub emulator does not exist or is not running.

函数在functions/scraper/index.js中定义为:

exports.scraper = url => {
  console.log('TEST TEST', url)
  return null
}

它在functions/cron/index.js中被导入和调用

const functions = require('firebase-functions')
const test = require('../scraper')

const schedule = `every 6 hours`
exports.testFunction = functions.pubsub
  .schedule(schedule)
  .onRun(test.scraper('123'))

我怎么会看到这个函数的TEST TEST 123 输出而不是我的其他函数?如何避免在运行模拟器时执行此功能?

我正在尝试在本地测试计划的 cron 功能,但它在模拟器运行时一直在执行。

【问题讨论】:

  • 把它变成箭头函数怎么样:.onRun(()=>test.scraper('123'))

标签: javascript node.js firebase google-cloud-functions firebase-cli


【解决方案1】:

通过编写.onRun(test.scraper('123')),您要求pubsub 运行test.scraper('123')返回值。这就是在环境初始化期间调用该函数的原因。

如果你想每 6 小时运行一次test.scraper('123'),你必须把它包装成一个函数。

exports.testFunction = functions.pubsub
  .schedule(schedule)
  .onRun(() => test.scraper('123'))

为了更清楚:

exports.testFunction = functions.pubsub
  .schedule(schedule)
  .onRun(scheduledFunction)

function scheduledFunction() {
  test.scraper('123')
}

【讨论】:

  • 你答案的第二部分是我已经拥有的。我很困惑。
  • 也许你有.onRun(scheduledFunction())而不是.onRun(scheduledFunction)
  • 哦,甚至没有注意到。谢谢!
猜你喜欢
  • 2020-02-05
  • 1970-01-01
  • 1970-01-01
  • 2021-11-09
  • 2017-07-06
  • 1970-01-01
  • 2017-02-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多