Reputation: 80
I'm trying to get the below snippet to work, but for some reason, I can't get the script to recognize the "element" part of the "attributes.States.element" line.
Is it because there is a "." in the line?
If I am to make the first line of the forEach section "console.log(element);", it works perfectly.
If I use it as laid out below it fails not recognizing "element".
This snippet is being used in Node.js 8.10 in an Alexa skill.
** Edited to include entire Handler statement in the Alexa code.
Any help would be appreciated!
const HelloWorldIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'HelloWorldIntent';
},
async handle(handlerInput) {
const attributesManager = handlerInput.attributesManager;
const attributes = await attributesManager.getPersistentAttributes() || {};
const speakOutput = 'Hello World!';
attributes.States = {
};
const stateNames = ['Alabama', 'New York'];
stateNames.forEach(element => {
attributes.States.element = {
'found' : 'no',
'history' : 'no'
};
});
attributesManager.setPersistentAttributes(attributes);
await attributesManager.savePersistentAttributes();
return handlerInput.responseBuilder
.speak(speakOutput)
//.reprompt('add a reprompt if you want to keep the session open for the user to respond')
.getResponse();
}
};
Upvotes: 1
Views: 117
Reputation: 1871
const HelloWorldIntentHandler = {
canHandle(handlerInput) {
return (
Alexa.getRequestType(handlerInput.requestEnvelope) === "IntentRequest" &&
Alexa.getIntentName(handlerInput.requestEnvelope) === "HelloWorldIntent"
);
},
async handle(handlerInput) {
const attributesManager = handlerInput.attributesManager;
const attributes =
(await attributesManager.getPersistentAttributes()) || {};
const speakOutput = "Hello World!";
// Add the following condition to avoid changing the data if something is returned from the getPersistentAttributes()
if (!attributes.States) {
attributes.States = {};
}
const stateNames = ["Alabama", "New York"];
stateNames.forEach(element => {
attributes.States[element] = {
found: "no",
history: "no"
};
});
attributesManager.setPersistentAttributes(attributes);
await attributesManager.savePersistentAttributes();
return (
handlerInput.responseBuilder
.speak(speakOutput)
//.reprompt('add a reprompt if you want to keep the session open for the user to respond')
.getResponse()
);
}
};
Upvotes: 1
Reputation: 146
If you wish to refer to a property named 'Alabama' or 'New York' in attributes.States
then you should use attributes.States[element]
.
The difference here is that the element
is being used as a value and not as a property name.
Upvotes: 2