我正在尝试使用 Shopify 订单和图片链接制作一张表格。但是异步循环有问题。每次我运行的代码顺序都以错误的顺序出现在控制台中,比如 998, 996, 1000... 应该是 1000, 999, 998... 我尝试在不同的地方添加 async 关键字并包装一个函数在 settimeout 功能和更多,但没有运气。如何按正确的顺序制作订单列表?先感谢您。
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: 'name.myshopify.com',
apiKey: 'key',
password: 'pas'
});
const sheets = google.sheets({version: 'v4', auth: client});
const drive = google.drive({version: 'v3', auth: client});
function getLink(){
client.authorize(()=> gsrun());
}
async function runDrive(ord, sku){
const resDrive = await drive.files.list({
pageSize: 1,
q: `name contains "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 {
// console.log('No files found.');
link = 'No files found.';
}
console.log([ord, sku, link])
}
async function gsrun(){
// Shopify - Get orders list
let orders = await shopify.order.list({ limit: 7 });
ordersArray = orders.forEach(ord => {
skuArray = ord.line_items.forEach(async (item) => {
// Drive - Search for SKUs
runDrive(ord.order_number, item.sku)
})
})
}
getLink()