@ google-cloud/bigquery查询API在Cloud Function中使用时返回空的Promise [英] @google-cloud/bigquery Query API returning empty Promise when used in Cloud Function
问题描述
我正在尝试使用'@ google-cloud/bigquery'库从Google Cloud函数查询BigQuery.当Promise返回时,我得到的只是一个包含另一个空数组的数组,即使当我从Big Query控制台运行相同的查询时,我也得到了一个非空的响应.
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.
我尝试使用异步函数代替Promise,但这并不成功.我还为我的服务帐户授予了"BigQuery Admin"和"Editor"特权,但这两种方法都没有解决.
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.
我确实知道API达到了大查询"水平.当我尝试通过Cloud Function创建新数据集时,该调用工作正常,但由于某种原因,我无法从BQ获取查询结果.
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);
});
}
}
我应该获得一个带有值的JSON result
对象,但是我只获取和一个带有空数组[[]]
I should get a JSON result
object with values, but I only get and array with an empty array [[]]
推荐答案
请使用可用于测试查询的公共数据集找到一个有效的示例
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)
}
}
})
这篇关于@ google-cloud/bigquery查询API在Cloud Function中使用时返回空的Promise的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!