将两个值传递给函数

时间:2017-03-26 21:50:47

标签: javascript reactjs react-redux

我正在为react-redux编写一个动作,我试图将两个变量传递给函数,一个dispatch,另一个hosts。问题是,host是console.log'作为函数而不是值。

我的代码:

export function hostSelected (hostSelected) {

    console.log(" host is2: ", hostSelected)

    let host = hostSelected

    return function(dispatch, host) {

        console.log(" host is: ", host)
       return eSclient.search({

            index: "sdpjmx",
            type: "kafkajmx",
            body:  {
                    "size": 0,
                    "aggs" : {
                        "topics" : {
                            "terms" : {
                              "field" : host,
                              "size": 500
                            }
                        }
                    },  
                    "query": {
                    "bool": {
                      "filter": [
                        {
                          "range": {
                            "@timestamp": {
                              "gte": "now-1d"
                            }
                          }
                        }
                        ]
                      }
                    }
                }

        }).then(topics => {

            dispatch(loadTopicsSuccess(topics));

        }).catch(error => {
            throw(error);
        });
    };

}

,控制台日志输出为:

console.log(" host is2: ", hostSelected)
host is2: hostname123

console.log(" host is: ", host)
host is:  function getState() {
    return currentState;
  }

为什么host会返回一个函数?

------- -------- EDIT

我将其更改为以下内容,但我仍然得到相同的结果....

export function hostSelected (hostSelection) {

    console.log(" host is2: ", hostSelection)

    let host = hostSelection

    return function(dispatch, host) {

        console.log(" host is: ", host)
        console.logt("host function: ", host())

       return eSclient.search({

            index: "sdpjmx",
            type: "kafkajmx",
            body:  {
                    "size": 0,
                    "aggs" : {
                        "topics" : {
                            "terms" : {
                              "field" : host,
                              "size": 500
                            }
                        }
                    },  
                    "query": {
                    "bool": {
                      "filter": [
                        {
                          "range": {
                            "@timestamp": {
                              "gte": "now-1d"
                            }
                          }
                        }
                        ]
                      }
                    }
                }

        }).then(topics => {

            dispatch(loadTopicsSuccess(topics));

        }).catch(error => {
            throw(error);
        });
    };

}

-

console.logt("host function: ", host())
host function: Object {topic_drpDwn: Array(0), initial_drpDwn: Object} //this is the current state object it returns

2 个答案:

答案 0 :(得分:0)

这是一个'语法错误'如果您将...在下面的示例中,第一个日志(1)将记录功能代码; function hi(){ return 'Hello world!'; },第二个(2)将记录Hello world!

function hi () {
    return 'Hello world!';
}

console.log(hi); //  1

console.log(hi()); //  2

因此,如果您将行更改为console.log(host());,则应该有效。

答案 1 :(得分:0)

你在redux-thunk中的动作创建者可以返回函数,然后用两个参数调用这些函数,dispatch将是第一个,getState将是第二个 - 这两个都是功能。当您记录host时,您看到的函数是redux-thunk传递的getState函数。

您(即hostSelected)传递给动作创建者的参数也可用于返回的函数,在闭包内。这意味着您可以将代码编写为:

export function hostSelected (host) {
    return function(dispatch) {
        eSclient.search({
            index: "sdpjmx",
            type: "kafkajmx",
            body:  {
                    "size": 0,
                    "aggs" : {
                        "topics" : {
                            "terms" : {
                              "field" : host,
                              "size": 500
                            }
                        }
                    },  
                    "query": {
                    "bool": {
                      "filter": [
                        {
                          "range": {
                            "@timestamp": {
                              "gte": "now-1d"
                            }
                          }
                        }
                        ]
                      }
                    }
                }
        }).then(topics => {
            dispatch(loadTopicsSuccess(topics));
        }).catch(error => {
            throw(error);
        });
    };
}

或使用ES6 lambda / arrow函数:

export const hostSelected = ( host ) => ( dispatch ) => {
    eSclient.search({
                index: "sdpjmx",
                type: "kafkajmx",
                body:  {
                        "size": 0,
                        "aggs" : {
                            "topics" : {
                                "terms" : {
                                  "field" : host,
                                  "size": 500
                                }
                            }
                        },  
                        "query": {
                        "bool": {
                          "filter": [
                            {
                              "range": {
                                "@timestamp": {
                                  "gte": "now-1d"
                                }
                              }
                            }
                            ]
                          }
                        }
                    }
            }).then(topics => {
                dispatch(loadTopicsSuccess(topics));
            }).catch(error => {
                throw(error);
            });
}