I am following all the steps presented in this link but it does not seem to be sending me anywhere thus I am asking for help here. I have a sample function that I want to first test and then deploy to firebase cloud function but its failing with this message:
ErrorHTTP/1.1 400 Bad Request x-powered-by: Express content-security-policy: default-src 'none' x-content-type-options: nosniff content-type: text/html; charset=utf-8 content-length: 1295 date: Mon, 27 Dec 2021 13:05:48 GMT connection: close
SyntaxError: Unexpected token ' in JSON at position 0
at JSON.parse (<anonymous>)
at createStrictSyntaxError (C:\Users\janic\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\body-parser\lib\types\json.js:158:10)
at parse (C:\Users\janic\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\body-parser\lib\types\json.js:83:15)
at C:\Users\janic\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\body-parser\lib\read.js:121:18
at invokeCallback (C:\Users\janic\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\raw-body\index.js:224:16)
at done (C:\Users\janic\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\raw-body\index.js:213:7)
at IncomingMessage.onEnd (C:\Users\janic\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\raw-body\index.js:273:7)
at IncomingMessage.emit (node:events:402:35)
at endReadableNT (node:internal/streams/readable:1343:12)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
I do not have any syntax error in the code, I literally copied the code from the link.
// 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;
});
and I invoke it from the console using this command:
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X POST -d '{"data":{}}' http://localhost:5001/bleconnect/us-central1/listProducts
Edit:
The error indicates there is something wrong with the parsing, however, even after trying to return a simple string "Test", it still throws the same error.
gcloud functions call, or the testing tab in the console.