简体   繁体   中英

Node.js: forEach arrow statement drama

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM