【问题标题】:Application works less than 24 hours and alway throws an error应用程序运行时间少于 24 小时并且总是抛出错误
【发布时间】:2019-10-10 14:47:03
【问题描述】:

这是一个应用程序,每 20 分钟将最新的 250 个订单从 Shopify 呈现到 Google 表格。 (ubuntu, node js, 数字海洋)

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

它工作不到 24 小时并且总是抛出这个错误: 这是我的第一个应用程序。 如何解决?我应该在外面的某个地方运行脚本吗? 提前致谢。

【问题讨论】:

  • 我在错误中说您尚未处理 Promise. 中的错误它还指出这将终止 Node.js。您必须确定哪些 Promise 没有被正确处理并在那里解决问题。我不确定 Google 表格以及它在做什么,而且我在您提供的代码中看不到任何承诺。
  • @daddygames 每个await 调用都隐含地期望一个Promise。
  • @Pointy 谢谢 :) 所以这仍然意味着其中一个 await 调用未能实现它是 Promise. 我认为我在这方面是正确的。
  • 是的。很难从 OP 中看到错误的详细信息,但显然应该有一个 try / catch 围绕某物。
  • 谢谢!包裹在 try / catch 之后,它可以工作几天而不会中断。

标签: javascript node.js async-await shopify digital-ocean


【解决方案1】:

包裹在 try / catch 之后,它可以在不中断的情况下工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-07
    • 1970-01-01
    • 1970-01-01
    • 2017-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多