
// [START app]
'use strict';

// [START setup]


//const BigQuery = require('@google-cloud/bigquery');
const projectId = "doolinhomeiot";
const datasetId = "OfficeData";
const tableId = "iotdata";
var request = "{auth: oath2Client,'projectId': " +  projectId + ", 'datasetId': " +  datasetId + ", 'tableId': " + tableId + ", 'resource': {'kind': 'bigquery#tableDataInsertAllRequest','rows':[{'insertId': 123456, 'json': '{'nameid': '123','messagedata':'test1'}'}]}}";
console.log(request);
var express = require('express');
var app = express();
var google = require('googleapis');
var bigquery = google.bigquery('v2');

app.enable('trust proxy');

var Datastore = require('@google-cloud/datastore');
// Instantiate a datastore client
var datastore = Datastore();

var PubSub = require('@google-cloud/pubsub');

//Instantiate a pubsub client
var pubsub = PubSub();

//The following environment variables are set by app.yaml when running on GAE,
//but will need to be manually set when running locally.
var PUBSUB_VERIFICATION_TOKEN = process.env.PUBSUB_VERIFICATION_TOKEN;

var topic = pubsub.topic(process.env.PUBSUB_TOPIC);

var subscription = pubsub.subscription(process.env.PUBSUB_SUBSCRIPTION_NAME);
google.auth.getApplicationDefault(function(err, authClient) {
    if (err) {
        console.log('Authentication failed because of ', err);
        return;
    }
    if (authClient.createScopedRequired && authClient.createScopedRequired()) {
        var scopes = ['https://www.googleapis.com/auth/cloud-platform'];
        authClient = authClient.createScoped(scopes);
    }
    })
//'insertId': 123456, 'json': '{"nameid": 123,"messagedata":"test1"

//var request = {projectId: projectId,datasetId: datasetId,tableID: tableId,resource: {"kind": "bigquery#tableDataInsertAllRequest","rows":
//    {}, auth: authClient]};

// [END setup]

    function storeEvent(message) {
        var key = datastore.key('officedata');
        key.identifier = "id"
        // You can uncomment some of the other things if you want to store them in the database

        var obj = {
            messageid: message.id
            //deviceid: message.attributes.device_id,
            ////event: message.attributes.event,
            //published: message.attributes.published_at,
            //data: message.data
        }
        datastore.save(
            {
                key: key,
                data: obj
            }, function (err) {
                if (err) {
                    console.log('There was an error storing the event', err);
                }
                console.log('stored in datastore', obj);
            }
        );
        var response = "response";
        var request = "{auth: oath2Client,'projectId': projectId, 'datasetId': datasetId, 'tableId': tableId, 'resource': {'kind': 'bigquery#tableDataInsertAllRequest','rows':[{'insertId': 123456, 'json': '{'nameid': '123','messagedata':'test1'}'}]}}";
        //save to BigQuery Tables
        debug.log(request);
        bigquery.tabledata.insertAll(request, function(err, result) {
            if (err) {
                console.log(err);
            } else {
                console.log(result);
            }
        });


        //err: result});


        subscription.on('message', function (message) {
            console.log('event received', message);
            // Called every time a message is received.
            // message.id = ID used to acknowledge its receival.
            // message.data = Contents of the message.
            // message.attributes = Attributes of the message.
            storeEvent(message);
            message.ack();
        });

// [START listen]
        var server = app.listen(process.env.PORT || 8080, function () {
            console.log('App listening on port %s', server.address().port);
            console.log('Press Ctrl+C to quit.');
        });
// [END listen]
// [END app]

        module.exports = app;
    }

@@ -46,450 +46,8 @@
 p%5D%0A%0A
-%0A//const BigQuery = require('@google-cloud/bigquery');%0Aconst projectId = %22doolinhomeiot%22;%0Aconst datasetId = %22OfficeData%22;%0Aconst tableId = %22iotdata%22;%0Avar request = %22%7Bauth: oath2Client,'projectId': %22 +  projectId + %22, 'datasetId': %22 +  datasetId + %22, 'tableId': %22 + tableId + %22, 'resource': %7B'kind': 'bigquery#tableDataInsertAllRequest','rows':%5B%7B'insertId': 123456, 'json': '%7B'nameid': '123','messagedata':'test1'%7D'%7D%5D%7D%7D%22;%0Aconsole.log(request);%0A
 var 
@@ -72,24 +72,25 @@
 'express');%0A
+%0A
 var app = ex
@@ -253,16 +253,165 @@
 tore');%0A
+//const BigQuery = require('@google-cloud/bigquery');%0Aconst projectId = %22doolinhomeiot%22;%0Aconst datasetId = %22OfficeData%22;%0Aconst tableId = %22iotdata%22;%0A%0A
 // Insta
@@ -1284,15 +1284,12 @@
    %7D
-%0A    
 %7D)
+;%0A
 %0A//'
@@ -2339,24 +2339,26 @@
  );%0A        
+//
 var response
@@ -2426,16 +2426,20 @@
 jectId':
+%22 + 
  project
@@ -2440,16 +2440,20 @@
 rojectId
+ + %22
 , 'datas
@@ -2458,16 +2458,20 @@
 asetId':
+%22 + 
  dataset
@@ -2472,16 +2472,20 @@
 atasetId
+ + %22
 , 'table
@@ -2488,24 +2488,28 @@
 ableId':
+%22 + 
  tableId
 , 'resou
@@ -2500,16 +2500,20 @@
  tableId
+ + %22
 , 'resou

Update app.js