应用程序工作不到 24 小时并且总是抛出错误
Application works less than 24 hours and alway throws an error
此应用每 20 分钟从 Shopify 将最新的 250 个订单渲染到 google 表。
(ubuntu,节点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();
它工作不到二十四小时,总是抛出这个错误:
这是我的第一个应用程序。
如何解决?我应该 运行 脚本在外面的某个地方吗?
提前致谢。
在 try / catch 中包装后,它可以在不中断的情况下工作。
此应用每 20 分钟从 Shopify 将最新的 250 个订单渲染到 google 表。 (ubuntu,节点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();
它工作不到二十四小时,总是抛出这个错误:
在 try / catch 中包装后,它可以在不中断的情况下工作。