地图内部的递归函数未定义

时间:2018-12-14 14:01:50

标签: javascript

我有以下内容:

export const normaliseHistoryLog = history => {

    if (history.children.length === 0) {
        debugger;
        return {
            id: history.id,
            date: history.timestamp, 
            time: history.timestamp,
            event: history.event.label,
            eventHTML: getHTML(history.event.detail), 
            source: history.source,
            requestor: history.requestor
        }
    }
    debugger;
    return {
        id: history.id,
        date: history.timestamp, 
        detail: history.children.map(normaliseHistoryLog),
        time: history.timestamp,
        event: history.event.label,
        eventHTML: getHTML(history.event.detail), 
        source: history.source,
        requestor: history.requestor
    }
}

我对此有误

detail: history.children.map(normaliseHistoryLog),

说“ normaliseHistoryLog”未定义。

我尝试了以下方法:

detail: history.children.map((el) => normaliseHistoryLog(el))

但仍然没有运气。

该函数称为:

export const getHistory = asyncMiddleware(async (req) => {
    const path = `url`;
    const response = await get(req, path);
    debugger;
    return response.result.map(normaliseHistoryLog);
});

其中response.result是对象数组

1 个答案:

答案 0 :(得分:-1)

TL; DR;

function normaliseHistoryLog(history) {
    if (history.children.length === 0) {
        debugger;
        return {
            id: history.id,
            date: history.timestamp, 
            time: history.timestamp,
            event: history.event.label,
            eventHTML: getHTML(history.event.detail), 
            source: history.source,
            requestor: history.requestor
        }
    }
    debugger;
    return {
        id: history.id,
        date: history.timestamp, 
        detail: history.children.map(normaliseHistoryLog),
        time: history.timestamp,
        event: history.event.label,
        eventHTML: getHTML(history.event.detail), 
        source: history.source,
        requestor: history.requestor
    }}
    export function normaliseHistoryLog;

这是由于吊装。 声明函数normaliseHistoryLog的方式称为函数表达式。 即使导出了函数表达式(基本上是将函数分配为变量),也要在稍后阶段完成。 这意味着在创建闭包时(声明函数并进行分配时)不知道(不可用)要使用的函数。

您可以通过在JS中查找提升来了解更多信息。 例如这里: https://gomakethings.com/function-expressions-vs-function-declarations/