【发布时间】:2018-07-25 18:04:08
【问题描述】:
I'm replicating this Google authored tutorial 我遇到了一个我不知道如何解决的问题和错误。
在 Google Cloud Function import json to bigquery 上,我收到错误“TypeError: job.promise is not a function”
位于函数底部,有问题的代码是:
.then(([job]) => job.promise())
The error led me to this discussion about the API used,但我不明白如何解决错误。
我尝试了.then(([ job ]) => waitJobFinish(job)),删除该行解决了错误,但没有插入任何内容。
第三个问题:我也找不到有关如何触发功能测试的文档,以便我可以在谷歌云功能控制台中阅读我的 console.logs,这将有助于解决这个问题。我可以测试这个函数的 json POST 部分,但是我找不到什么 json 来触发一个新文件写入云存储的测试 - 测试说必须包含一个存储桶,但我不知道要格式化什么 json(我用来测试帖子的 json -> 存储到云存储不起作用)
这是我将其提取到它自己的函数中的完整函数:
(function () {
'use strict';
// Get a reference to the Cloud Storage component
const storage = require('@google-cloud/storage')();
// Get a reference to the BigQuery component
const bigquery = require('@google-cloud/bigquery')();
function getTable () {
const dataset = bigquery.dataset("iterableToBigquery");
return dataset.get({ autoCreate: true })
.then(([dataset]) => dataset.table("iterableToBigquery").get({ autoCreate: true }));
}
//set trigger for new files to google storage bucket
exports.iterableToBigquery = (event) => {
const file = event.data;
if (file.resourceState === 'not_exists') {
// This was a deletion event, we don't want to process this
return;
}
return Promise.resolve()
.then(() => {
if (!file.bucket) {
throw new Error('Bucket not provided. Make sure you have a "bucket" property in your request');
} else if (!file.name) {
throw new Error('Filename not provided. Make sure you have a "name" property in your request');
}
return getTable();
})
.then(([table]) => {
const fileObj = storage.bucket(file.bucket).file(file.name);
console.log(`Starting job for ${file.name}`);
const metadata = {
autodetect: true,
sourceFormat: 'NEWLINE_DELIMITED_JSON'
};
return table.import(fileObj, metadata);
})
.then(([job]) => job.promise())
//.then(([ job ]) => waitJobFinish(job))
.then(() => console.log(`Job complete for ${file.name}`))
.catch((err) => {
console.log(`Job failed for ${file.name}`);
return Promise.reject(err);
});
};
}());
【问题讨论】:
标签: javascript google-bigquery google-cloud-functions