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,

1 Like

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();

2 Likes

@ms4446 Thanks lot’s

1 Like