我是新手,目前正在学习 SuiteScript。我无法理解为什么没有上下文值从reduce 函数传递到summary 函数。日志结果显示上下文值,除了: {"type":"mapreduce.Summary","dateCreated":"2023-08-29T17:02:27.014Z","seconds":9,"usage": 190,"concurrency":5,"yields":0}",这导致发生错误。但是,Reduce Context 日志按预期显示值。
有人愿意帮助我了解我做错了什么或我应该做什么吗?预先感谢您的协助;我真的很感激。
问候
我的代码示例如下,我期望从reduce阶段获取'fileList'上下文到summary阶段。
代码示例如下:
function reduce(context) {
try {
var transactions = JSON.parse(context.values[0]); // Parse the file contents loaded from getInputData
log.debug('Reduce Context', transactions); // This logs the values as expected
if (transactions && transactions.length > 0) {
// Iterate through file transactions array
transactions.forEach(function (transaction) {
/** .... performing other tasks with transaction object
.........to create and save journal
*/
// Build the file list array and write context that needs to be moved
context.write({
key: "fileList",
value: {
fieldId: transaction.fileId,
duplicate: transaction.duplicate,
failed: transaction.failed
}
});
});
}
} catch (e) {
log.error('Reduce Error', 'Error in Reduce function => ' + e.name + ' : ' + e.message);
}
}
function summarize(context) {
try {
log.debug('Summarized Context', context); // The log results show nothing for the context value
fileList = JSON.parse(context.values[0]); // Parse the context values loaded from reduce stage
if (fileList && fileList.length > 0) {
//Iterate through the file list array and move each file to the relevant folder
fileList.forEach(function (file) {
var fileObj = file.load({
id: file.fileID
});
if (file.duplicate === "Y") {
fileObj.folder = DUPLICATE_FILE_FOLDER_ID; // Move the file from Pending Folder to Duplicate Folder
} else if (file.failed === "Y") {
fileObj.folder = FAILED_FILE_FOLDER_ID; // Move the file from Pending Folder to Failed Folder
} else {
fileObj.folder = PROCESSED_FILE_FOLDER_ID; // Move the file from Pending folder to Processed Folder
}
fileObj.save();
});
}
} catch (err) {
log.audit('File Moving Error', err.name + ' : ' + err.message);
}
}
summarize 使用不同的上下文。没有值成员。
对于您所呈现的内容,您想要类似的内容:
const fileRefs = [];
ctx.output.iterator().each((key, value)=>{
if(key == 'fileList'){
var fileRef = JSON.parse(value);
fileRefs.push(fileRef);
return true;
}
});
fileRefs.
filter(uniques(ref=>ref.fileId)). // create a function uniques that only returns true for the first instance of fileId NOTE your code also used fieldId where it looks like it should use fileId
forEach(ref=>{
... your original logic
});