【问题标题】:Can I pass parameters of a function as part of a query in bigquery?我可以在 bigquery 中将函数的参数作为查询的一部分传递吗?
【发布时间】:2019-03-17 23:13:07
【问题描述】:
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)UnhandledPromiseRejectionWarning:错误:未声明的查询参数

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

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

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

【问题讨论】:

  • 如果你用一个字符串代替p1,你会得到一个不同的错误吗?我想知道p1 是否没有定义。根据the example in the tests,您的代码看起来是正确的。
  • 调用测试函数时p1的值是多少?
  • @ElliottBrossard p1 是字符串,例如)'PWG'
  • @vinoaj p1 是字符串,例如)'PWG'

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


【解决方案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

【讨论】:

    【解决方案2】:

    如果查询返回结果,您的代码会抛出 ReferenceError “url is not defined”,因为您使用的是“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")
    

    【讨论】:

    • 谢谢您,但是您为什么回答与我类似的答案。习惯性地对一个可以的答案进行投票,而不是添加重复项。 Tnk
    • 问候,我认为答案是不同的,因为你说问题出在使用“console.log(url)”,但我说错误“未声明的查询参数”不相关对此,它说问题出在没有必要参数的情况下调用函数“test”。
    • 你是对的,问题中描述的代码中有 2 个不同的错误。通常是在可能的情况下添加 cmets 并避免多个答案的好习惯。谢谢你。随意投票:)
    • 在问题中描述的代码中,我看到了这两个错误: - 没有用括号关闭“forEach”。 - 您指出的错误,“console.log(url)”。该代码缺少对函数“test”的调用,因此我不得不使用“test()”和“test(“UnionOfRAD”)”重现该问题,以得出“未声明的查询参数”错误是由以下原因引起的结论没有必要参数的调用,但我认为代码没有明确显示调用这一事实不是错误,因为它是一个 sn-p。
    • 是的,这就是为什么在我的回答中我提供了一个完整的 mocha 测试,您可以复制/粘贴并执行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-11
    • 1970-01-01
    • 2013-08-08
    • 2023-04-01
    • 2020-06-09
    相关资源
    最近更新 更多