首页 > 解决方案 > 如何在本地获取测试数据运行功能?

问题描述

我有以下内容:

export const helloWorld = functions.https.onRequest((request, response) => {
    response.send(request.body);
});

我在本地运行它并运行helloWorld("Hey"),这是输出:

firebase > helloWorld('HEY')
Sent request to function.
firebase > info: User function triggered, starting execution
info: Execution took 1 ms, user function completed successfully

RESPONSE RECEIVED FROM FUNCTION: 200, "{}"

为什么只有{}当我清楚地发送一个字符串时它才输出?

标签: google-cloud-functionsfirebase-cli

解决方案


这不是您在本地调用 HTTP 类型函数的方式。您应该查看文档并使用其中建立的模式。您调用该方法,就好像您正在使用节点请求模块一样:

在 shell 中调用 HTTPS 函数,用法与 request NPM 模块相同,但将 request 替换为您要模拟的函数的名称。例如:

# invoke
myHttpsFunction()
myHttpsFunction.get()
myHttpsFunction.post()

# invoke at sub-path
myHttpsFunction('/path')
myHttpsFunction.get('/path')
myHttpsFunction.post('/path')

# send POST request with form data
myHttpsFunction.post('/path').form( {foo: 'bar' })

我不确定您是否能够指定整个内容正文。这似乎是一种不常见的情况,因为您通常通过其查询字符串或表单编码主体将参数传递给 HTTP 函数。


推荐阅读