我有一个简单的nodejs脚本,可以将数据插入到bigquery表中。当数据输入正确时,它就像一个魅力。当数据输入不正确时,我没有得到错误响应。
如何处理错误响应?
'use strict'
// Imports the Google Cloud client library
const BigQuery = require('@google-cloud/bigquery');
//Instantiates a client
const bigquery = BigQuery({
projectId: 'projectid',
keyFilename: './credentials/cloudkey.json'
});
const datasetId = 'datasetId'
const tableId = 'test'
const rows = [
{"data": "data"}, //correct row
{"dtaa": "test"} // incorrect row
]
insertRowsAsStream(datasetId, tableId, rows)
.then( insertErrors => {
console.log(insertErrors)
})
function insertRowsAsStream (datasetId, tableId, rows) {
// References an existing dataset, e.g. "my_dataset"
const dataset = bigquery.dataset(datasetId);
// References an existing dataset, e.g. "my_dataset"
const table = dataset.table(tableId);
//console.log(table)
// Inserts data into a table
return table.insert(rows)
.then((insertErrors) => {
console.log('Inserted:');
rows.forEach((row) => console.log(row));
return insertErrors;
});
}
答案 0 :(得分:1)
insertRowsAsStream(datasetId, tableId, rows)
.catch( insertErrors => {
console.log(insertErrors)
})