1

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:

HTTP/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

Error
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.

1
  • When testing functions, I suggest to also try the tools for testing: gcloud functions call, or the testing tab in the console. Commented Jan 28, 2022 at 11:15

1 Answer 1

2

The data being returned has most likely already been parsed, is not JSON at all, or is an incorrectly stringified JSON string.

Sign up to request clarification or add additional context in comments.

3 Comments

that's what the error message assumes but it doesn't necessarily mean that. I have tried to return only a string "Test" but it throws exactly the same error.
Yes, because just "Test" isn't correct JSON either. You'd have to return something along the lines of "{"test":true}".
@showtime does this answer helped you? if it helped you could you please upvote it or accept it to help the community? Thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.