Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

getting issue with async await with node js client

Hi all,
I am using 

"@google-cloud/bigquery": "^6.2.0"

 https://github.com/googleapis/nodejs-bigquery

I want to create delete the table data and Insert into same table.

import { BigQuery } from '@google-cloud/bigquery';

async function runQuery_truncate_table() {
    console.log(" this is  truncate_table")
    const query = 'TRUNCATE TABLE `ghistory`';
    const options = {
        query: query,
    };

    const [job] = await bigquery.createQueryJob(options);
    console.log(`Job ${job.id} Deleted .`);

}

// main run
//  let dt1 = await runQuery_truncate_table().catch(console.error);
// //setInterval(runQuerydelete_table, 1500);


async function main() {
      await runQuery_truncate_table();   
// await datainsert();
 }
  main();


client truncate run after the data insert. it was not happens all time, 

Solved Solved
1 2 1,258
1 ACCEPTED SOLUTION

Please try the below updated function:

import { BigQuery } from '@google-cloud/bigquery';

const bigquery = new BigQuery();

async function runQuery_truncate_table() {
console.log("This is truncate_table")
const query = 'TRUNCATE TABLE `your-project-id.your-dataset-id.ghistory`';
const options = {
query: query,
};

const [job] = await bigquery.createQueryJob(options);
await job.getQueryResults();
console.log(`Job ${job.id} executed .`);

}

async function main() {
await runQuery_truncate_table();
// await datainsert();
}
main();

View solution in original post

2 REPLIES 2

Please try the below updated function:

import { BigQuery } from '@google-cloud/bigquery';

const bigquery = new BigQuery();

async function runQuery_truncate_table() {
console.log("This is truncate_table")
const query = 'TRUNCATE TABLE `your-project-id.your-dataset-id.ghistory`';
const options = {
query: query,
};

const [job] = await bigquery.createQueryJob(options);
await job.getQueryResults();
console.log(`Job ${job.id} executed .`);

}

async function main() {
await runQuery_truncate_table();
// await datainsert();
}
main();

@ms4446   Thanks lot's