Javascript 节点脚本无法完成
我有一个相当简单的节点脚本。它从API中提取数据,循环遍历所有数据,并编译2个报告。一个是JSON格式,另一个是CSV格式。然后对数据进行排序,最后导出一个JSON文件和两个CSV文件(副本) 我已将日志记录到脚本中,发现脚本无法前进到或超过在将数组导出为文件格式之前对数组进行排序的部分 这可能是什么原因造成的 **值得注意的是,如果我删除JSON或CSV底部的排序和导出,脚本将完全运行。它只是不能同时做这两件事。但更奇怪的是,它上周在完全相同的配置下工作,但突然中断Javascript 节点脚本无法完成,javascript,node.js,Javascript,Node.js,我有一个相当简单的节点脚本。它从API中提取数据,循环遍历所有数据,并编译2个报告。一个是JSON格式,另一个是CSV格式。然后对数据进行排序,最后导出一个JSON文件和两个CSV文件(副本) 我已将日志记录到脚本中,发现脚本无法前进到或超过在将数组导出为文件格式之前对数组进行排序的部分 这可能是什么原因造成的 **值得注意的是,如果我删除JSON或CSV底部的排序和导出,脚本将完全运行。它只是不能同时做这两件事。但更奇怪的是,它上周在完全相同的配置下工作,但突然中断 const { xm, x
const { xm, xm_env, uncaught_endpoint, prod_xm } = require('./../config');
const util = require('./../node_modules/xmtoolbox/lib/util');
const path = require('path');
const workflow = process.argv.slice(2);
const fs = require('fs')
const pathname = './../../../Logs/';
const filename = path.basename(__filename, '.js');
let fields = [
'sub_name',
'sub_uuid',
'sub_form_name',
'sub_form_uuid',
'workflow_name',
'workflow_uuid',
'owner_targetName',
'owner_uuid',
'owner_firstName',
'owner_lastName',
'recipient_targetName',
'recipient_uuid',
'recipient_firstName',
'recipient_lastName',
];
(async (env, workflow) => {
try {
const report = [];
const json = [];
const subscriptions = await xm.subscriptions.getMany(env);
util.buildLogger(pathname, filename).info('Pulled in all the subscriptions...');
await Promise.all(subscriptions.map(async sub => {
if (sub.criteria) {
let sub_obj = {
sub_name : sub.name,
sub_uuid : sub.id,
sub_form_name : sub.form.name,
sub_form_uuid : sub.form.id,
workflow_name : sub.form.plan.name,
workflow_uuid : sub.form.plan.id,
owner_targetName : sub.owner.targetName,
owner_uuid : sub.owner.id,
owner_firstName : sub.owner.firstName,
owner_lastName : sub.owner.lastName
};
let json_obj = {
recipients : [],
id : sub.id,
name : sub.name,
form : sub.form,
owner : sub.owner,
created : sub.created,
criteria : sub.criteria.data,
description : sub.description,
notificationDelay : sub.notificationDelay
};
report.push(sub_obj);
if (sub.recipients && sub.recipients.count > 1) {
let recipients = await xm.subscriptions.getSubscribers(env, '', sub.id);
await Promise.all(recipients.map(async r => {
json_obj.recipients.push({ targetName: r.targetName, id : r.id })
let recip_obj = {
sub_name : sub.name,
sub_uuid : sub.id,
sub_form_name : sub.form.name,
sub_form_uuid : sub.form.id,
workflow_name : sub.form.plan.name,
workflow_uuid : sub.form.plan.id,
owner_targetName : sub.owner.targetName,
owner_uuid : sub.owner.id,
owner_firstName : sub.owner.firstName,
owner_lastName : sub.owner.lastName,
recipient_targetName : r.targetName,
recipient_firstName : r.firstName,
recipient_lastName : r.lastName,
recipient_uuid : r.id
};
}
json.push(json_obj);
}
}));
util.buildLogger(pathname, filename).info('Looped through all subscriptions and built report arrays...');
// -------------- LOGGING NEVER MAKES IT PAST THIS PART ---------------------- //
let sorted_report = await util.awaitSort(report, ['form_name', 'sub_name']);
util.buildLogger(pathname, filename).info('Built Sorted Report...');
let sorted_json = await util.awaitSort(json, ['name']);
util.buildLogger(pathname, filename).info('Built sorted_json Report...');
fs.writeFileSync('./../../../Feeds/Subs_JSON.json', JSON.stringify(sorted_json))
util.buildCSV(fields, sorted_report, 'Subscriptions');
util.buildCSV(fields, sorted_report, `${util.today()}_Subscriptions`, true);
util.buildLogger(pathname, filename).info('All files written...');
} catch (e) {
util.uncaught_exception_webhook(xm_env, path.basename(__filename), workflow, uncaught_endpoint, e);
}
})(prod_xm, workflow);
等待来自UTIL文件的排序函数
async function awaitSort(data, keys) {
let sorted_data = data.sort((a, b) => {
keys.map(i => {
if (a[i].toLowerCase() < b[i].toLowerCase()) {
return -1
}
if (a[i].toLowerCase > b[i].toLowerCase()) {
return 1;
}
return 0;
});
});
return sorted_data;
}
异步函数等待排序(数据、键){
让排序的数据=数据。排序((a,b)=>{
key.map(i=>{
if(a[i].toLowerCase()b[i].toLowerCase()){
返回1;
}
返回0;
});
});
返回排序后的数据;
}
订阅。map
不返回承诺。所以,wait Promise.all(subscriptions.map
无法解析。您只需为(let sub of subscriptions){…执行,请发送util.wait排序代码…实际上,您的.map()
中没有一个返回任何承诺。购买wait Promise.all()
它们。为什么?当然是Promise.all()
如果您传递的数组不包含承诺,它将永远无法解析。好吧,但是如果我删除脚本中与JSON(JSON、JSON_obj、JSON_obj.recipients.push等)相关的内容,会怎么样呢如果我删除这些引用,脚本工作得很好。当我添加另一个JSON东西时,它就会崩溃。也许我没有正确使用承诺,我这样做是为了在脚本完成之前,我可以处理大量数据,而不必跳到脚本的下一部分。到目前为止,它对我来说工作得很好……我添加了排序功能关于代码,@JeremyThille