微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么运行的Firebase模拟器也执行我的功能之一?

如何解决为什么运行的Firebase模拟器也执行我的功能之一?

我运行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')),您要求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')
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?