我可以在bigquery中将函数的参数作为查询的一部分传递吗?

时间:2019-03-14 02:05:46

标签: node.js google-cloud-platform google-bigquery

async function test(p1) {
  let [result] = await bigquery.query({
    query: [
      'SELECT url',
      'FROM `publicdata.samples.github_nested`',
      'WHERE repository.owner = @owner'

    ].join(' '),
    params: {
      owner: p1
    }
  });
  result.forEach(row => {
    console.log(url)
  }
}
  

错误:(节点:19492)UnhandledPromiseRejection警告:错误:未声明的查询参数

要编写大型查询,请在查询中传递函数的参数 我使用了bigquery提供的查询语句。在node.js环境中。 但是,我无法保存存储p1的@owner。

以下文档与此相关。 https://cloud.google.com/nodejs/docs/reference/bigquery/2.0.x/BigQuery

在bigquery中作为查询的一部分发送函数的参数 我该怎么办? node.js环境。

2 个答案:

答案 0 :(得分:1)

您的问题是您做了console.log(url)而不是console.log(row.url)

这是我用来验证此代码的完整代码

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

import BigQuery from '@google-cloud/bigquery'

describe('Check BigQuery', async () => {

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

    })

    async function test(p1) {
        try {
            const bigquery = new BigQuery({
                projectId: `myProject`,
                keyFilename: '/Users/tamirklein/ssh/9473b308ab0e.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 => {
                console.log(`row number ${index}, url is: ${row.url}`)
            })
        } catch (err) {
            console.log("err", err)
        }
    }
})

这是输出:

query is: SELECT url FROM `publicdata.samples.github_nested` WHERE repository.owner = @owner
row number 0, url is: https://github.com/de3/Panada
row number 1, url is: https://github.com/schbern/samples
row number 2, url is: https://github.com/panada/Panada/compare/47a1801f13...9dedbc8ce6
row number 3, url is: https://github.com/panada/Panada/pull/38
row number 4, url is: https://github.com/panada/samples/compare/46934664ea...a7cae9f088
row number 5, url is: https://github.com/panada/Panada/compare/175c88e2cb...47a1801f13
row number 6, url is: https://github.com/panada/samples/compare/1a35a44548...46934664ea
row number 7, url is: https://github.com/panada/samples/compare/256c9b4ed3...1f293ca245
row number 8, url is: https://github.com/panada/documentation/compare/49c38b23e2...d948d2eb97
row number 9, url is: https://github.com/panada/Panada/pull/38
row number 10, url is: https://github.com/panada/documentation/compare/d948d2eb97...46b7bcde5f
row number 11, url is: https://github.com/panada/samples/compare/a7cae9f088...256c9b4ed3
row number 12, url is: https://github.com/panada/Panada/pull/36
row number 13, url is: https://github.com/panada/documentation/pull/1
row number 14, url is: https://github.com/panada/documentation/pull/1
row number 15, url is: https://github.com/de3/documentation
row number 16, url is: https://github.com/cakyus/Panada
row number 17, url is: https://github.com/panada/Panada/compare/9dedbc8ce6...4db3e50d80
row number 18, url is: https://github.com/panada/documentation/compare/46b7bcde5f...52e9ef5c67
row number 19, url is: https://github.com/panada/documentation/issues/1#issuecomment-4533276

答案 1 :(得分:1)

如果查询返回结果,则您的代码将引发ReferenceError“未定义URL”,因为您使用的是“ console.log(url)”而不是“ console.log(row.url)”。

错误“未声明的查询参数”与此无关。这意味着您在调用函数“ test”时没有必要的参数,例如“ test()”。您必须向其传递一个字符串,例如'test(“ UnionOfRAD”)'。

对我有用的测试代码:

const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();

async function test(p1) {
  let [result] = await bigquery.query({
    query: [
      'SELECT url',
      'FROM `publicdata.samples.github_nested`',
      'WHERE repository.owner = @owner'

    ].join(' '),
    params: {
      owner: p1
    }
  });
  result.forEach(row => {
    console.log(row.url)
  })
}
test("UnionOfRAD")