1

I want to use a Google Cloud Function with a http trigger to write data into a Google Spreadsheet.

The following code is my cloud function:

exports.writeDataMaterialCollection = functions.https.onRequest(
  (req, res) => {
    if (req.method === "POST") {
      console.log(req.body);
      res.set("Access-Control-Allow-Origin", "*");
      const sheets = google.sheets({ version: "v4" });
      var jwt = getJwt();
      var apiKey = getApiKey();
      var spreadsheetId = "sheetIDxxxxxxxxx";
      var range = "A:L";
      var row = ["data"];
      sheets.spreadsheets.values.append(
        {
          spreadsheetId: spreadsheetId,
          range: range,
          auth: jwt,
          key: apiKey,
          valueInputOption: "RAW",
          resource: { values: [row] }
        },
        (err, result) => {
          if (err) {
            throw err;
          } else {
            console.log(result.data.updates.updatedRange);
            res.status(200).send(result.data.updates.updatedRange);
          }
        }
      );
    }
  }
);

When I make a curl POST request, then the data is written to the spreadsheet correctly.

url -d '{"test": "wert"}' -X POST http://localhost:5001/XXXX/writeDataMaterialCollection

Problem

What I don't understand is when I use Axios inside Vue.js, Google Cloud Function returns throw new Error("Function timed out.")

axios(
        "http://localhost:5001/XXXXX/writeDataMaterialCollection",
        {
          method: "POST",
          headers: {
            "content-type": "application/json",
            "Access-Control-Allow-Origin": "*"
          },
          data: {
            daten1: 23
          }
        }
      ).then(response => (self.writeDataResult = response));

1 Answer 1

2

If your function gets to the point of this line:

throw err;

It will not actually terminate the function and propagate that error to the client. That's because you're throwing an error out of a callback function, not the main function. This also means that function will timeout, because no response is being sent to the client.

What you should do instead is send an error to the client, so the function can correctly terminate, and the client can receive the error. You might want to consider logging the error as well, so you can see in the console what went wrong:

if (err) {
  res.send(500);
  console.error(err);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.