![](/img/trans.png)
[英]“Converting circular structure to JSON” BigQuery Insert from Cloud Function NodeJs
[英]Google cloud function bigquery json insert TypeError: job.promise is not a function
我正在复制这个Google撰写的教程 ,遇到了一个问题和错误,无法解决。
在Google Cloud Function将json导入bigquery时,出现错误“ TypeError:job.promise不是函数”
它位于函数底部,相关代码为:
.then(([job]) => job.promise())
该错误导致我进行了有关所用API的讨论 ,但我不知道如何解决该错误。
我尝试了.then(([ job ]) => waitJobFinish(job))
,删除该行可解决该错误,但不插入任何内容。
第三大问题:我也找不到有关如何触发功能测试的文档,因此我可以在Google Cloud Function Console中阅读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);
});
};
}());
所以我不知道如何解决谷歌的例子,但是我能够从js获得此负载以与谷歌云函数中的以下代码一起工作:
'use strict';
/*jshint esversion: 6 */
// 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')();
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;
}
const importmetadata = {
autodetect: false,
sourceFormat: 'NEWLINE_DELIMITED_JSON'
};
let job;
// Loads data from a Google Cloud Storage file into the table
bigquery
.dataset("analytics")
.table("iterable")
.import(storage.bucket(file.bucket).file(file.name),importmetadata)
.then(results => {
job = results[0];
console.log(`Job ${job.id} started.`);
// Wait for the job to finish
return job;
})
.then(metadata => {
// Check the job's status for errors
const errors = metadata.status.errors;
if (errors && errors.length > 0) {
throw errors;
}
})
.then(() => {
console.log(`Job ${job.id} completed.`);
})
.catch(err => {
console.error('ERROR:', err);
});
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.