0
votes

I try to get data from a google bigquery database in my firebase cloud funtions.

exports.corsEnabledFunction = functions.https.onRequest((req, res) => {

  res.set("Access-Control-Allow-Origin", "*");

  if (req.method === "OPTIONS") {
    // Send response to OPTIONS requests
    res.set("Access-Control-Allow-Methods", "GET");
    res.set("Access-Control-Allow-Headers", "Content-Type");
    res.set("Access-Control-Max-Age", "3600");
    res.status(204).send("");
  } else {
    // Set CORS headers for the main request

    res.set("Access-Control-Allow-Origin", "*");
    let bigquery = new BigQuery();
    const query = "SELECT * FROM `mydb-2094d.logs.eventlogs` LIMIT 10";
    bigquery.query(query, (err, results) => {
       if (err) {
           console.log("error", err);
       }

       console.log("results", results);
       res.send(results);
    }
   });
  }
});

results are always null. The table is not empty.

1
Could you edit the question to show the entire minimal function that doesn't work the way you expect, along with all logs that it generates? - Doug Stevenson
I edited but don't think that it helps - daniel
Could you show the logs as well? - Doug Stevenson

1 Answers

2
votes

Looking at the BigQuery.query() definition it expects the callback to have signature (err, rows), so it looks like your callback is looking at the error rather than the actual results. Try:

bigquery.query(query, (err, results) => {
    console.log("results", results);
    res.send(results);
}