【发布时间】:2022-01-26 10:46:12
【问题描述】:
我正在遵循link 中提供的所有步骤,但它似乎没有将我发送到任何地方,因此我在这里寻求帮助。我有一个示例函数,我想先对其进行测试,然后部署到 Firebase 云函数,但它失败并显示以下消息:
错误HTTP/1.1 400 错误请求 x-powered-by: Express content-security-policy: default-src 'none' x-content-type-options: nosniff 内容类型:文本/html; charset=utf-8 内容长度:1295 日期:2021 年 12 月 27 日星期一 13:05:48 GMT 连接:关闭
SyntaxError: Unexpected token ' in JSON at 位置 0
在 JSON.parse ()
在 createStrictSyntaxError (C:\Users\janic\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\body-parser\lib\types\json.js:158:10)
解析时 (C:\Users\janic\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\body-parser\lib\types\json.js:83:15)
在 C:\Users\janic\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\body-parser\lib\read.js:121:18
在调用回调 (C:\Users\janic\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\raw-body\index.js:224:16)
完成时 (C:\Users\janic\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\raw-body\index.js:213:7)
在 IncomingMessage.onEnd (C:\Users\janic\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\raw-body\index.js:273:7)
在 IncomingMessage.emit (节点:事件:402:35)
在 endReadableNT(节点:内部/流/可读:1343:12)
在 processTicksAndRejections (node:internal/process/task_queues:83:21)
我在代码中没有任何语法错误,我从链接中复制了代码。
// functions/index.js
const functions = require('firebase-functions');
const faker = require('faker');
// Initialize products array
const products = [];
// Max number of products
const LIMIT = 100;
// Push a new product to the array
for (let i = 0; i < LIMIT; i++) {
products.push({
name: faker.commerce.productName(),
price: faker.commerce.price(),
});
}
exports.listProducts = functions.https.onCall((data, context) => {
return products;
});
我使用以下命令从控制台调用它:
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X POST -d '{"data":{}}' http://localhost:5001/bleconnect/us-central1/listProducts
编辑:
该错误表明解析有问题,但是,即使尝试返回一个简单的字符串“Test”,它仍然会抛出相同的错误。
【问题讨论】:
-
在测试功能的时候,建议也试试测试工具:
gcloud functions call,或者控制台的测试标签。
标签: node.js firebase google-cloud-functions