创建回调以从函数返回数组
Creating a callback to return an array from a function
我正在尝试学习Node.js
我在创建自己的函数回调时遇到问题。这似乎是一件简单的事情,但我不太明白该怎么做。
该函数传递一个地址(例如:"1234 will ln,co"),该地址使用 google 的地理定位 json api 返回数组中的完整地址、纬度和经度。
这是我的代码:
//require secure http module
var https = require("https");
//My google API key
var googleApiKey = "my_private_api_key";
//error function
function printError(error) {
console.error(error.message);
}
function locate(address) {
//accept an address as an argument to geolocate
//replace spaces in the address string with + charectors to make string browser compatiable
address = address.split(' ').join('+');
//var geolocate is the url to get our json object from google's geolocate api
var geolocate = "https://maps.googleapis.com/maps/api/geocode/json?key=";
geolocate += googleApiKey + "&address=" + address;
var reqeust = https.get(geolocate, function (response){
//create empty variable to store response stream
var responsestream = "";
response.on('data', function (chunk){
responsestream += chunk;
}); //end response on data
response.on('end', function (){
if (response.statusCode === 200){
try {
var location = JSON.parse(responsestream);
var fullLocation = {
"address" : location.results[0].formatted_address,
"cord" : location.results[0].geometry.location.lat + "," + location.results[0].geometry.location.lng
};
return fullLocation;
} catch(error) {
printError(error);
}
} else {
printError({ message: "There was an error with Google's Geolocate. Please contact system administrator"});
}
}); //end response on end
}); //end https get request
} //end locate function
所以当我尝试执行我的函数时
var testing = locate("7678 old spec rd");
console.dir(testing);
控制台日志未定义,因为它没有等待从定位返回(或者至少我猜这是问题所在)。
如何创建回调,以便当定位函数返回我的数组时,它会在返回的数组上运行 console.dir。
谢谢!我希望我的问题有意义,我是自学成才的,所以我的技术术语很糟糕。
您需要将
回调函数传递给您的方法 - 因此回调可能如下所示
function logResult(fullLocation){
console.log(fullLocation)
}
您将它与输入一起传递给您的locate
方法:
// note: no parentheses, you're passing a reference to the method itself,
// not executing the method
locate("1234 will ln, co",logResult)
您也可以内联执行此操作 - 就像您已经在处理的response
对象一样:
locate("1234 will ln, co",function(fullLocation){
// do something useful here
})
现在对于方法中的位,与其尝试return
结果,不如调用带有结果的回调:
function locate(address, callback) {
......
response.on('end', function (){
if (response.statusCode === 200){
try {
var location = JSON.parse(responsestream);
var fullLocation = {
"address" : location.results[0].formatted_address,
"cord" : location.results[0].geometry.location.lat + "," + location.results[0].geometry.location.lng
};
callback(fullLocation); // <-- here!!!
} catch(error) {
printError(error);
}
} else {
printError({ message: "There was an error with Google's Geolocate. Please contact system administrator"});
}
}); //end response on end
.....
}
相关文章:
- Regex提取URL返回数组的一部分;未定义”;
- 为循环嵌套的Javascript未按预期返回数组
- 从函数中的函数返回数组时出错
- JSON返回数组的奇怪结果
- 如何从NodeJS中的模块返回数组
- 在JS中返回数组的大整数运算
- 从Nodejs中的函数返回数组,并将其发送到ejs视图
- 从函数返回数组
- undercore返回数组中对象的indes,其中单词存在于对象中的句子中
- Yui Get 复选框返回数组而不是布尔值
- 从异步调用返回数组,然后为数组的每个元素返回其他异步调用
- JavaScript闭包和返回数组元素
- 按流星空格键模板中的索引返回数组项
- 从多个文件中推送文本的函数(使用html5文件读取器上传)返回数组
- 我想返回数组
- PHP和Ajax在回调和返回数组中使用foreach循环
- Javascript-如何通过以字符串作为参数返回数组
- 从JSON调用SQL返回数组时出现问题
- Jasmine spyOn使用伪造的返回数组测试功能
- 无法从从sql数据库读取的javascript函数中获取返回数组