【问题标题】:In a function return values are not assigning to the variable [duplicate]在函数中,返回值未分配给变量[重复]
【发布时间】:2018-07-27 19:29:29
【问题描述】:

我正在尝试使用带有 getMidPoint 函数的多边形来查找三个不同位置的三个中点。同时尝试在 getMidPoint 函数中返回数组。数组值未分配给 midPoints 变量,我在终端中无法读取未定义的属性“长度”作为错误

var midPoints = getMidPoint(Location1, Location2)
    // Directions API
    var i = 0;
    var array = [];
    function getMidPoint(a, b) {
        console.log("I am working...")
        clientMap.post(config_KEYS.DIRECTION_API + a + "&destination=" + b + "&key=" + config_KEYS.GOOGLE_API_KEY + "&departure_time=" + timeStamp + "", function (googleDirectionData, error) {
            if (googleDirectionData.status == "ZERO_RESULTS" | googleDirectionData.status == "INVALID_REQUEST") {
                res.status(400).send(googleDirectionData)
            }
            else {
                var polyline = googleDirectionData.routes[0].overview_polyline.points
                var decoded = decode(polyline)                
                var n = Math.round(decoded.length / 2)
                var obj = {}
                obj.latitude = decoded[n].latitude
                obj.longitude = decoded[n].longitude
                array.push(obj)   
            }  
        })
        return array    
    }

    if (midPoints && midPoints.length == 1) {
        console.log("durationpointDat   dfggdfg"+ midPoints.length)
        var durationpointData2 = getMidPoint(Location2, Location3)
    }
    else {
        if (midPoints.length == 2) {
            console.log("durationpointData fdf"+ midPoints.length)
            var durationpointData3 = getMidPoint(Location3, Location1)                        
        }
        else{
                res.status(200).json(array)
        }
    }

我们将不胜感激。

【问题讨论】:

    标签: javascript arrays node.js express


    【解决方案1】:

    正如我在getMidPoint 的代码中看到的那样,您正在调用谷歌地图 API。它是 asynchronous 调用意味着在完成 Google API 调用之前,您的代码正在执行下一个代码,因此在 midPoints 中,您将变得未定义。

    表示您在完成 google map api 之前返回数组。您必须创建一个回调并将数据传递给它。

    你可以按照这个例子。

    var midPoints;
    getMidPoint(Location1, Location2, function(error,data){
        // You can check for error here
        midPoints = data;
    })
    // Directions API
    var i = 0;
    var array = [];
    function getMidPoint(a, b, callback) {
        console.log("I am working...")
        clientMap.post(config_KEYS.DIRECTION_API + a + "&destination=" + b + "&key=" + config_KEYS.GOOGLE_API_KEY + "&departure_time=" + timeStamp + "", 
            function (googleDirectionData, error) {
            if (googleDirectionData.status == "ZERO_RESULTS" | googleDirectionData.status == "INVALID_REQUEST") {
                res.status(400).send(googleDirectionData)
            } else {
                var polyline = googleDirectionData.routes[0].overview_polyline.points
                var decoded = decode(polyline)                
                var n = Math.round(decoded.length / 2)
                var obj = {}
                obj.latitude = decoded[n].latitude
                obj.longitude = decoded[n].longitude
                array.push(obj)
                callback(null, array);
            }  
        }) 
    }
    

    【讨论】:

      【解决方案2】:

      尝试在函数中声明 var 数组并返回它。不在全局范围内

      【讨论】:

      • 是的,我在函数中分配了数组,但是返回数组语句的值没有分配给midPoint并且中点长度未定义
      猜你喜欢
      • 1970-01-01
      • 2021-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-19
      • 1970-01-01
      • 1970-01-01
      • 2017-09-23
      相关资源
      最近更新 更多