Willy
Willy

Reputation: 81

No error response for streaming data into BigQuery

I have a simple nodejs script which inserts data into a bigquery table. It works like a charm when the data input is correct. When the data input isn't correct I do not get a error response.

How can I handle the error response?

'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;
    });
}

Upvotes: 1

Views: 424

Answers (1)

Willy
Willy

Reputation: 81

stupid mistake! I should have used the catch function after insertRowAsStream!

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

Upvotes: 2

Related Questions