Javascript Node.js:forEach arrow语句
我正试图让下面的代码段正常工作,但由于某些原因,我无法让脚本识别“attributes.States.element”行中的“element”部分。Javascript Node.js:forEach arrow语句,javascript,node.js,alexa-skills-kit,Javascript,Node.js,Alexa Skills Kit,我正试图让下面的代码段正常工作,但由于某些原因,我无法让脚本识别“attributes.States.element”行中的“element”部分。 是因为行中有一个“.”吗? 如果我要创建forEach部分的第一行“console.log(element);”,它工作得非常好。 如果我按如下所示使用它,它将无法识别“元素”。 这段代码正在Alexa skill中的Node.js 8.10中使用。 **编辑以在Alexa代码中包含整个处理程序语句 任何帮助都将不胜感激 const HelloWo
是因为行中有一个“.”吗?
如果我要创建forEach部分的第一行“console.log(element);”,它工作得非常好。
如果我按如下所示使用它,它将无法识别“元素”。
这段代码正在Alexa skill中的Node.js 8.10中使用。
**编辑以在Alexa代码中包含整个处理程序语句 任何帮助都将不胜感激
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();
}
};
如果希望在
attributes.States
中引用名为“Alabama”或“New York”的属性,则应使用attributes.States[element]
这里的区别在于
元素
被用作值而不是属性名。如果您希望在属性中引用名为“Alabama”或“New York”的属性。States
则应使用属性。States[元素]
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()
);
}
};
这里的区别在于
元素
被用作值而不是属性名。什么是属性
?我没有看到任何定义为属性的内容
发布完整的代码片段,或者尝试为每个元素控制台第一个和第二个。这就是您查找属性的原因。States[element]
??@sagar.acharya可能是,也可能不是,但应该发布相关数据,以便人们能够帮助他找到问题的确切原因,与其猜测,不如将相关数据与问题一起发布IMO@codeManiac同意!:d什么是属性
?我没有看到任何定义为属性的内容
发布完整的代码片段,或者尝试为每个元素控制台第一个和第二个。这就是您查找属性的原因。States[element]
??@sagar.acharya可能是,也可能不是,但应该发布相关数据,以便人们能够帮助他找到问题的确切原因,与其猜测,不如将相关数据与问题一起发布IMO@codeManiac同意!:谢谢你,这确实是答案。行中的“.”是个问题,因为它根本不应该在那里!非常感谢。谢谢你,这确实是答案。行中的“.”是个问题,因为它根本不应该在那里!非常感谢。这是修复,谢谢萨加尔!我想我错过了一些简单的东西。这就是解决办法,谢谢萨加尔!我想我错过了一些简单的东西。
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()
);
}
};