Node.js 参数未传递给回调函数
Node.js param not being passed to callback function
我正在创建 Alexa 技能(Lambda 函数)node.js - 我在将 "zipcode" const 传递给 getLocation 回调函数时遇到问题,如果我输出邮政编码,它将工作。
getLocation 函数没有返回任何内容,我猜这是因为 zipcode 参数没有正确传递到函数中。
函数没有任何问题,因为如果我替换
var url = "localhost/api.php?zip="+邮政编码;
和
var url = "localhost/api.php?zip=41242";或它有效的任何邮政编码。
我做错了什么?
let zipcode = request.intent.slots.zipcode.value;
getLocation(function(location, err) {
if(err) {
context.fail(err);
} else {
options.speechText += location.distance + " miles away,
You can get there by going to " + location.street_address + " in " + location.city + ", " + location.state;
options.endSession = true;
context.succeed(buildResponse(options));
}
});
function getLocation(callback, zipcode) {
var url = "localhost/api.php?zip="+zipcode;
var req = http.get(url, function(res) {
var body = "";
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
body = body.replace(/\/g, '');
var location = JSON.parse(body);
callback(location);
});
res.on('error', function(err) {
callback('', err);
});
});
}
在 getLocation() 函数中,您应该将邮政编码作为第二个参数传递。你的代码将是这样的:
getLocation(function(location, err) {
*/you code here /*
} , zipcode);
我正在创建 Alexa 技能(Lambda 函数)node.js - 我在将 "zipcode" const 传递给 getLocation 回调函数时遇到问题,如果我输出邮政编码,它将工作。
getLocation 函数没有返回任何内容,我猜这是因为 zipcode 参数没有正确传递到函数中。
函数没有任何问题,因为如果我替换
var url = "localhost/api.php?zip="+邮政编码;
和
var url = "localhost/api.php?zip=41242";或它有效的任何邮政编码。
我做错了什么?
let zipcode = request.intent.slots.zipcode.value;
getLocation(function(location, err) {
if(err) {
context.fail(err);
} else {
options.speechText += location.distance + " miles away,
You can get there by going to " + location.street_address + " in " + location.city + ", " + location.state;
options.endSession = true;
context.succeed(buildResponse(options));
}
});
function getLocation(callback, zipcode) {
var url = "localhost/api.php?zip="+zipcode;
var req = http.get(url, function(res) {
var body = "";
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
body = body.replace(/\/g, '');
var location = JSON.parse(body);
callback(location);
});
res.on('error', function(err) {
callback('', err);
});
}); }
在 getLocation() 函数中,您应该将邮政编码作为第二个参数传递。你的代码将是这样的:
getLocation(function(location, err) {
*/you code here /*
} , zipcode);