Ich erstelle eine Alexa-Fähigkeit (Lambda-Funktion) node.js - Und ich habe ein Problem in der "PLZ" Const übergeben, um die Callback-Funktion getLocation, wenn ich ausgeben die Postleitzahl funktioniert.Node.js Param nicht an Callback-Funktion übergeben
Die getLocation-Funktion gibt nichts zurück, und ich vermute, es liegt daran, dass der Postleitzahlparameter nicht ordnungsgemäß in die Funktion übernommen wird.
Es ist nichts falsch mit der Funktion, denn wenn ich ersetzen die
var url = "localhost/api.php zip =?" + Zipcode;
mit
var url = "localhost/api.php zip = 41242"; oder irgendeine Postleitzahl arbeitet es.
Was mache ich falsch?
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);
});
}); }
Wenn Sie * getLocation * aufrufen, übergeben Sie nur ein Argument - den Callback -, sodass das Argument * PLZ * immer * undefiniert * ist. –
@JulianGoacher wie soll ich das dann angehen? – koepitz
Nun, wenn die Postleitzahl etwas wie "abc123" ist, dann möchten Sie den Funktionsaufruf in 'getLocation (Funktion (location, err) {/ * ... Callback Code ... * /," abc123 ") ändern' ' - dh es als zweites Argument in den Funktionsaufruf aufnehmen. –