googlecodelabs / iot-data-pipeline

Apache License 2.0
18 stars 7 forks source link

Parse error #2

Closed rnishku closed 6 years ago

rnishku commented 6 years ago

I get this error when I try to validate with JSONlint.com Code seems to fail in GC. Anyone else getting the same issue?

The error

Error: Parse error on line 1:

exports.subscribe = -^ Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'

The code below

/**
 * Background Cloud Function to be triggered by PubSub.
 *
 * @param {object} event The Cloud Functions event.
 * @param {function} callback The callback function.
 */
exports.subscribe = function (event, callback) {
  const BigQuery = require('@google-cloud/bigquery');
  const projectId = "myProject"; //Enter your project ID here
  const datasetId = "myDataset"; //Enter your BigQuery dataset name here
  const tableId = "myTable"; //Enter your BigQuery table name here -- make sure it is setup correctly
  const PubSubMessage = event.data;
  // Incoming data is in JSON format
  const incomingData = PubSubMessage.data ? Buffer.from(PubSubMessage.data, 'base64').toString() : "{'sensorID':'na','timecollected':'1/1/1970 00:00:00','zipcode':'00000','latitude':'0.0','longitude':'0.0','temperature':'-273','humidity':'-1','dewpoint':'-273','pressure':'0'}";
  const jsonData = JSON.parse(incomingData);
  var rows = [jsonData];

  console.log(`Incoming data: ${rows}`);

  // Instantiates a client
  const bigquery = BigQuery({
    projectId: projectId
  });

  // Inserts data into a table
  bigquery
    .dataset(datasetId)
    .table(tableId)
    .insert(rows)
    .then((insertErrors) => {
      console.log('Inserted:');
      rows.forEach((row) => console.log(row));

      if (insertErrors && insertErrors.length > 0) {
        console.log('Insert errors:');
        insertErrors.forEach((err) => console.error(err));
      }
    })
    .catch((err) => {
      console.error('ERROR:', err);
    });
  // [END bigquery_insert_stream]

  callback();
};
sunsetmountain commented 6 years ago

Were the variables in the following lines of code changed to match your project, dataset and table?

const projectId = "myProject"; //Enter your project ID here const datasetId = "myDataset"; //Enter your BigQuery dataset name here const tableId = "myTable"; //Enter your BigQuery table name here -- make sure it is setup correctly

rnishku commented 6 years ago

Yes I did. Even if I didn't, it shouldn't fail on a json validator

sunsetmountain commented 6 years ago

While the line from the code does have issues in JSONlint.com, I just built everything from scratch and everything is working fine on GC.

gardiaz commented 6 years ago

This is the error I get when trying to check the Cloud function to see if the streaming data is coming in... with // gcloud beta functions logs read function-weatherPubSubToBQ// .. As instructed double check the projectID was changed (to my information) from the index.js portion... still no luck.

` at Request.emit (events.js:191:7) at Request. (/user_code/node_modules/@google-cloud/bigquery/node_modules/request/reque st.js:1157:10) at emitOne (events.js:96:13) at Request.emit (events.js:188:7) code: 400, errors: [ { message: 'Invalid project ID \' fifth-pact-211618\'. Project IDs must contain 6-63 lowercase l etters, digits, or dashes. IDs must start with a letter and may not end with a dash.', domain: 'global', reason: 'invalid' } ], response: undefined, message: 'Invalid project ID \' fifth-pact-211618\'. Project IDs must contain 6-63 lowercase letter s, digits, or dashes. IDs must start with a letter and may not end with a dash.' }

`