völlig neu zu Node.js und diese Callback-Sache macht mich verrückt.Node.js "Callback nicht definiert" beim Versuch, auf eine HTTP-Anfrage
Ich schreibe eine Fähigkeit für ein Amazon Echo. Als Teil versuche ich eine SMS mit BulkSMS.com über eine HTTP-Anfrage zu senden. Die http.request hat einen Callback, der die Antwort analysiert. (Um das BulkSMS API aus der Gleichung im folgenden Beispiel herauszunehmen, versuche ich nur, es mit einer http-Anfrage an Random.org zu arbeiten (zB www.random.org/integers/?num=1 & min = 1 & max = 10 & col = 1 & base = 10 & format = plain & rnd = new)
jedoch habe ich einen Fehler bin immer sagen, dass der Rückruf nicht definiert ist
{ "errormessage":. „Rückruf ist nicht definiert ", " errorType ":" ReferenceError ", " stackTrace ": [ " Emergency.e ventHandlers.onLaunch (/var/task/index.js:54:11) ", " AlexaSkill.requestHandlers.LaunchRequest (/var/task/AlexaSkill.js:16:37) ", " AlexaSkill.execute (/ var /task/AlexaSkill.js:97:24)“, "exports.handler (/var/task/index.js:100:15)" ] }
ich bin sicher, dass ich etwas tue, völlig dumm und dafür entschuldige ich mich, aber ich kann es einfach nicht sehen.
Hier ist der Ereignishandler onLaunch in meinem index.js. Jede Hilfe sehr geschätzt.
Emergency.prototype.eventHandlers.onLaunch = function (launchRequest, session, response) {
console.log("Emergency onLaunch requestId: " + launchRequest.requestId + ", sessionId: " + session.sessionId);
console.log("Attempting to send SMS");
callback = function(response) {
var str = '';
console.log("In callback");
//another chunk of data has been recieved, so append it to `str`
response.on('data', function (chunk) {
str += chunk;
console.log("Getting Data");
});
//the whole response has been recieved, so we just print it out here
response.on('end', function() {
console.log("End of response");
console.log(str);
});
}
http.request(options, callback).end();
console.log("Finished sending SMS");
var speechOutput = "<speak>SMS sent</speak>";
response.tell(speechOutput);
};
Vollständige index.js ist unten.
'use strict';
/**
* App ID for the skill
*/
var APP_ID = "amzn1.ask.skill.eb8cf94a-848f-45ae-9792-xxxxxxxxxx";
/**
* The AlexaSkill prototype and helper functions
*/
var AlexaSkill = require('AlexaSkill');
var http = require('http');
var request = require("request");
//var Alexa = require('alexa-sdk');
var SKILL_NAME = 'Emergency';
var Emergency = function() {
AlexaSkill.call(this, APP_ID);
console.log("APP_ID set");
};
var alexaResponse = "";
//The url we want is: 'www.random.org/integers/? num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new'
var options = {
host: 'www.random.org',
path: '/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new'
};
// Extend AlexaSkill
Emergency.prototype = Object.create(AlexaSkill.prototype);
Emergency.prototype.constructor = Emergency;
Emergency.prototype.eventHandlers.onSessionStarted = function (sessionStartedRequest, session) {
console.log("Emergency onSessionStarted requestId: " + sessionStartedRequest.requestId
+ ", sessionId: " + session.sessionId);
// any initialization logic goes here
};
Emergency.prototype.eventHandlers.onLaunch = function (launchRequest, session, response) {
console.log("Emergency onLaunch requestId: " + launchRequest.requestId + ", sessionId: " + session.sessionId);
console.log("Attempting to send SMS");
callback = function(response) {
var str = '';
console.log("In callback");
//another chunk of data has been recieved, so append it to `str`
response.on('data', function (chunk) {
str += chunk;
console.log("Getting Data");
});
//the whole response has been recieved, so we just print it out here
response.on('end', function() {
console.log("End of response");
console.log(str);
});
}
http.request(options, callback).end();
console.log("Finished sending SMS");
var speechOutput = "<speak>SMS sent</speak>";
response.tell(speechOutput);
};
Emergency.prototype.eventHandlers.onSessionEnded = function (sessionEndedRequest, session) {
console.log("Emergency onSessionEnded requestId: " + sessionEndedRequest.requestId
+ ", sessionId: " + session.sessionId);
// any cleanup logic goes here
};
Emergency.prototype.intentHandlers = {
// register custom intent handlers
EmergencyIntent: function (intent, session, response) {
// Get a random "never" phrase from the list
}
};
// Create the handler that responds to the Alexa Request.
exports.handler = function (event, context) {
// Create an instance of the Emergency skill.
var emergency = new Emergency();
emergency.execute(event, context);
};
Sie Code-Snippet scheint gut zu funktionieren. Ist das der vollständige Code? – Sridhar
Danke für einen Blick Sridhar - hier ist der vollständige Code. – pmcmurrough