【发布时间】:2018-12-07 05:06:49
【问题描述】:
Cloud 函数等待文件进入存储桶并将其加载到 BQ。 BQ 表已经存在。即使我在函数顶部分配了数据集名称,我的 datasetid 也是未定义的。关于我的问题的任何想法?
Following Function 返回以下错误。 错误:函数崩溃。细节: 无法读取未定义的属性“表”
{
"name": "sample-cloud-storage",
"version": "0.0.1",
"repository": "googleapis/nodejs-bigquery",
"engines": {
"node": ">=4"
},
"dependencies": {
"@google-cloud/bigquery": "1.2.0",
"@google-cloud/storage": "1.5.1",
"yargs": "10.0.3"
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "2.1.3",
"ava": "0.24.0",
"nyc": "11.3.0",
"proxyquire": "1.8.0",
"sinon": "4.1.3",
"uuid": "3.1.0"
}
}
exports.ToBigQuery_Stage = (event, callback) => {
//https://stackoverflow.com/questions/49111829/using-cloud-function-to-load-data-into-big-query-table-it-is-appending-to-the-t
const BigQuery = require('@google-cloud/bigquery');
const Storage = require('@google-cloud/storage');
const file = event.data;
const context = event.context;
const projectId = "bi-sales-sms";
const datasetId = "bi-sales-sms:BI_Sales_SMS";
const bucketName = file.bucket;
const filename = file.name;
//const dashOffset = filename.indexOf('-');
//const tableId = filename + "_STAGE";
const tableId = 'Roll_Up_Daily_Sales_Per_Retailer';
console.log('hello norman');
console.log('Load ' + filename + ' into ' + tableId);
const metadata = {
allowJaggedRows: true,
skipLeadingRows: 1
};
//test the print
console.error(BigQuery.Dataset(datasetId));
BigQuery
//issue is here
.Dataset(datasetId)
.table(tableId)
.Load(storage.bucket(bucketName).file(filename),metadata)
.then(results => {
job = results[0];
// 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);
});
callback();
};
【问题讨论】:
-
嗨@N0RM1NAT0R,我提供了一个我相信可以解决您的问题的答案。它对你有用吗?
标签: node.js google-cloud-platform google-bigquery google-cloud-functions