I am trying to fetch values from mongo db and populate the fetched values in the reply from alexa simulator.
The alexa sdk version used is the latest one v2.0, ask-sdk.
Testing was done by using both alexa-skill-local as well as using lambda function.
The below return statement is working as expected
return handlerInput.responseBuilder
.speak("test")
.reprompt("test")
.withSimpleCard('Innovation details', "test")
.getResponse();
Result with the above return statement is shown below
{
"body": {
"version": "1.0",
"response": {
"outputSpeech": {
"type": "SSML",
"ssml": "<speak>test</speak>"
},
"card": {
"type": "Simple",
"title": "Innovation details",
"content": "test"
},
"reprompt": {
"outputSpeech": {
"type": "SSML",
"ssml": "<speak>test</speak>"
}
},
"shouldEndSession": false
},
"sessionAttributes": {},
"userAgent": "ask-node/2.3.0 Node/v8.12.0"
}
}
I have written a function ops() which will retrieve the values from a mongodb hosted in cloud atlas. The retrieved string will be used as speech text and reprompt text. But since javascript is asynchronous, and there are mongoose calls involved, i tried writing async/await functions, callback hells and finally promise functions to return the db value. The db calls are inside the promise object and the return statement is inside .then() as shown below. The console statements are populating correct results in the console when run using alexa-skill-local and no error is reported in the console. Running via lambda function also is not producing any errors.
ops(handlerInput).then(function(data){
console.log("in promise then "+data.speechText);
var handlerInput=data.input;
return handlerInput.responseBuilder
.speak(data.speechText)
.reprompt(data.speechText)
.withSimpleCard('Innovation details', data.speechText)
.getResponse();
}) .catch(error => console.log(error));
The passed resolve object is:
var passer={
"input": handlerInput,
"speechText":speechText
};
When using alexa-skill-local , the console statements are working as expected but the return statement is not returning value to the alexa simulator. The returned JSON is as
{
"body": {
"version": "1.0",
"response": {},
"sessionAttributes": {},
"userAgent": "ask-node/2.3.0 Node/v8.12.0"
}
}
Please help me out in returning values to the alexa simulator
Thank you for your reponses. The responses helped me in figuring out the solution.
I put a return in front of the promise object. Now the intent handler is returning promise object.