@google-cloud/bigquery Query API returning empty P

2019-08-10 20:10发布

I'm trying to use the '@google-cloud/bigquery' library to query BigQuery from a Google Cloud function. When the Promise returns, all I get back is an Array with another empty Array inside it, even though when I run the same query from the Big Query console I get a non-empty response back.

I've tried using an async function instead of a promise, but that was not successful. I also gave my Service Account "BigQuery Admin" and "Editor" privileges but that has not worked out either.

I do know the API is hitting Big Query. When I tried creating a new dataset from my Cloud Function, that call worked just fine, but for some reason I'm unable to get query results back from BQ.

  function warningAndPreventionIntent(agent) {
    let userCountry = agent.parameters['geo-country'];
    console.log(String(userCountry[0]));

    const gotCountry = userCountry.length > 0;

    if(gotCountry) {
      agent.add('Im looking into your trip');

      const OPTIONS = {
              query: 'SELECT disease.name FROM `projectId.dataset.table`, unnest(disease) disease WHERE country = @country',
              timeoutMs: 10000,
              useLegacySql: false,
              params: {country: userCountry[0]}
      };

      return bigquery
      .query(OPTIONS)
      .then(results => {
          console.log(JSON.stringify(results[0]))
          const ROWS = results[0];

          let diseaseList = [];

          for(var row of ROWS) {
            diseaseList.push(row.name);
            console.log(diseaseList);
          }

          return true;

      })
      .catch(err => {
        console.error('ERROR:', err);
      });
    }
  }

I should get a JSON result object with values, but I only get and array with an empty array [[]]

3条回答
兄弟一词,经得起流年.
2楼-- · 2019-08-10 20:58

Please find a working example using a public dataset you can use to test your query with

if (!global._babelPolyfill) {
    var a = require("babel-polyfill")
}

import BigQuery from '@google-cloud/bigquery'

describe('Check google-cloud', async () => {

    it('Test query', async () => {
        let result = await test('panada')

    })

    async function test(p1) {
        try {
            const bigquery = new BigQuery({
                projectId: `projectId`,
                keyFilename: '/Users/tamirklein/ssh/mydata.json'
            })

            let query = [
                'SELECT url',
                'FROM `publicdata.samples.github_nested`',
                'WHERE repository.owner = @owner'

            ].join(' ')

            console.log(`query is: ${query}`)
            let [result] = await bigquery.query({
                query,
                params: {
                    owner: p1
                }
            })

            result.forEach((row, index) => {
                console.log(`row number ${index}, url is: ${row.url}`)
            })
        } catch (err) {
            console.log("err", err)
        }
    }
})
查看更多
对你真心纯属浪费
3楼-- · 2019-08-10 21:03

i am now getting the same thing. I updated my cloud function without changing the code and the behavior changed.

查看更多
我想做一个坏孩纸
4楼-- · 2019-08-10 21:05

Taking into account your comments that downgrading google/bigquery to 1.3.X helped you resolve this. It seems to me that you may have been affected by this Issue. It looks like it was fixed in this merge, so you may be able to upgrade to the latest API version without re-introducing the error.

查看更多
登录 后发表回答