2017-06-13 8 views
0

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); 
    }); 

}); }

+0

Wenn Sie * getLocation * aufrufen, übergeben Sie nur ein Argument - den Callback -, sodass das Argument * PLZ * immer * undefiniert * ist. –

+0

@JulianGoacher wie soll ich das dann angehen? – koepitz

+0

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. –

Antwort

0

In der getLocation() - Funktion sollten Sie die Postleitzahl als zweites Argument übergeben. Ihr Code wird in etwa so aussehen:

Verwandte Themen