将数据流式传输到BigQuery没有错误响应

时间:2017-04-06 18:43:12

标签: node.js error-handling google-bigquery

我有一个简单的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;
    });
}

1 个答案:

答案 0 :(得分:1)

愚蠢的错误!我应该在insertRowAsStream之后使用catch函数!

insertRowsAsStream(datasetId, tableId, rows)
.catch( insertErrors  => {
  console.log(insertErrors)
})