firebasegoogle-cloud-platformgoogle-bigquerygoogle-cloud-functionsfirebase-extensions

Firebase to Bigquery transform function error: invalid json response body


I'm trying to implement a transform function for the Firestore to BigQuery Stream extension but I'm getting this error:

Unhandled error FetchError: invalid json response body at https://us-east4-<PROJECT_ID>.cloudfunctions.net/<FUNCTION_NAME> reason: Unexpected token < in JSON at position 1

Here is the code:

const functions = require('@google-cloud/functions-framework');
functions.http('eventsTransformFunction', (req, res) => {
 
  const inputPayload = req.body;
    const inputData = inputPayload.data[0];
    const outputPayload = [{
        insertId: inputData.insertId,
        json: {
            timestamp: inputData.json.timestamp,
            event_id: inputData.json.event_id,
            document_name: inputData.json.document_name,
            document_id: inputData.json.document_id,
            operation: inputData.json.operation,
            data: JSON.stringify({ createdAt: { _seconds: 1664983515, _nanoseconds: 745000000 }, name: "Transformed Name" })
        },
    }]

    res.send({ data: outputPayload });
});

I even tried this and still got the same error:

const functions = require('@google-cloud/functions-framework');
functions.http('eventsTransformFunction', (req, res) => {
 
    res.send(req.body).status(200);
});

Solution

  • I didn't try your code but, at first sight, the inner code of your Cloud Function looks correct. The problem probably comes from the fact that you are not deploying a Cloud Function for Firebase.

    As a matter of facts

    const functions = require('@google-cloud/functions-framework'); 
    functions.http('eventsTransformFunction', (req, res) => {})
    

    is the way for writing Cloud Functions to be deployed via the Google Cloud Console.

    I would suggest to use a Cloud Function for Firebase, which has a different syntax and must be deployed through the Firebase CLI. You can follow the Cloud Functions section of the Firebase documentation.

    So, for example, by using Cloud Functions 2nd generation, your code should be as follows:

    const {
      onRequest,
    } = require("firebase-functions/v2/https");
    
    exports.eventsTransformFunction = onRequest((req, res) => {
      const inputPayload = req.body;
      const inputData = inputPayload.data[0];
      const outputPayload = [
        {
          insertId: inputData.insertId,
          json: {
            timestamp: inputData.json.timestamp,
            event_id: inputData.json.event_id,
            document_name: inputData.json.document_name,
            document_id: inputData.json.document_id,
            operation: inputData.json.operation,
            data: JSON.stringify({
              createdAt: { _seconds: 1664983515, _nanoseconds: 745000000 },
              name: "Transformed Name",
            }),
          },
        },
      ];
    
      res.send({ data: outputPayload });
    });