SailsJS:在我的控制器中我有两个函数,第一个是allvehicleRecord()和另一个函数getLocation(latlng),它使用请求模块从谷歌地理编码获取地址。 我的问题是我不能调用allvehicleRecord()中的getLocation(latlng)函数来获取dataArray。它归还给我“未定义” 需要帮助......
var request = require('request');
getvehicle:function(req,res){
function cb(data){
if(data){
var formatData=formatresult(data);
}
res.json(formatData);
};
try
{
if(!req.user.id)
{
console.log("nt defined");
}}catch(e){
console.log("exception");
return;
}
var userID=req.user.id;
var vehicleView = VehicleView.getvehicleStatus(userID,cb);
function formatresult(data)
{
var dataArray = new Array();
for(var i = 0; i < data.length; i++)
{
var Lat = data[i].Lat;
var Lon = data[i].Lon;
var latlng=Lat+','+Lon;
//getLocation
var dataAddress = getLocation(latlng);
console.log(dataAddress);
dataArray.push({dataAddress: dataAddress});
}
return dataArray;
};
}
function getLocation(latlng){ var latlong = latlng;
var url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" +latlong+ "&sensor=false";
request({url: url,
json: true
},function (error, data, textStatus) {
if (!error && data.statusCode === 200) {
// if (textStatus && textStatus.length > 0)
streetaddress=(textStatus);
var address = streetaddress.results[0].formatted_address;
console.log(address);
return address;
}
});
}