Ad

Application Works Less Than 24 Hours And Alway Throws An Error

This is the app for rendering latest 250 orders every 20 minutes from Shopify to google sheets. (ubuntu, node js, digital ocean)

const {google} = require('googleapis');
const keys = require('./keys.json');
const Shopify = require('shopify-api-node');
const client = new google.auth.JWT(
    keys.client_email,
    null,
    keys.private_key,
    ['https://www.googleapis.com/auth/spreadsheets',
    'https://www.googleapis.com/auth/drive.metadata.readonly']
);
const shopify = new Shopify({
  shopName: 'shopname.myshopify.com',
  apiKey: 'key',
  password: 'pas'
});
const sheets = google.sheets({version: 'v4', auth: client});
const drive = google.drive({version: 'v3', auth: client});

let links = [];

async function runDrive(ord, sku){
    const resDrive = await drive.files.list({
    pageSize: 1,
    q: sku,
    spaces: 'drive',
    });
    const files = resDrive.data.files;
    let link;
    if (files.length) {
      link = `https://drive.google.com/file/d/${files[0].id}/view`;
    } else {
      link = 'No files found.';
    }
    console.log([ord, sku, link])
    links.push([ord, sku, link])
}

async function gsrun(){
  links = [];
  
  // Shopify - Get orders list
  let orders = await shopify.order.list({ limit: 250 });

  for (const ord of orders){
    for(const item of ord.line_items){
      await runDrive(ord.order_number, item.sku)
    }
  }

  // Sheets - Update cells
  const clearRange = {
    spreadsheetId: 'spreadsheetId',
    range: 'Data!A3:G700'
  }
  const updateData = {
      spreadsheetId: 'spreadsheetId',
      range: 'Data!A3',
      valueInputOption: 'USER_ENTERED',
      resource: { values: links},
  };
  await sheets.spreadsheets.values.clear(clearRange);
  console.log('Sheet cleaned')
  await sheets.spreadsheets.values.update(updateData);
  console.log('Sheet updated')

  setTimeout(()=> {
    console.log('repeat');
    gsrun();
  }, 1200000);
}

gsrun();

It works less than twenty-four hours and alway throws this error: node js error It's my first application. How to fix it? Should I run the script somewhere outside? Thanks in advance.

Ad

Answer

After wrapping in try / catch it works without interrupting.

Ad
source: stackoverflow.com
Ad