gpt4 book ai didi

node.js - 无法读取未定义的 Alexa SDK 的属性 'getMonetizationServiceClient'

转载 作者:搜寻专家 更新时间:2023-11-01 00:19:35 24 4
gpt4 key购买 nike

我正在尝试使用以下代码将 In Skill Purchasing 添加到我的 Alexa 技能中:

const LaunchRequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === "LaunchRequest";
},
handle(handlerInput){
console.log("In LaunchRequest");

const locale = handlerInput.requestEnvelope.request.locale;
const ms = handlerInput.serviceClientFactory.getMonetizationServiceClient();

return ms.getInSkillProducts(locale).then(function(result) {
// Code to handle result.inSkillProducts goes here
const totalProducts = result.inSkillProducts.length;
const purchasableProducts = result.inSkillProducts.filter(record => record.purchasable == 'PURCHASABLE');
const entitledProducts = result.inSkillProducts.filter(record => record.entitled == 'ENTITLED');

return handlerInput.responseBuilder
.speak('Found total ' + result.inSkillProducts.length + ' products of which ' + purchasableProducts.length + ' are purchasable and ' + entitledProducts.length + ' are entitled.')
.getResponse();
});
},
};

当我运行代码时,我收到以下错误消息:

Response:
{
"errorMessage": "Cannot read property 'getMonetizationServiceClient' of undefined",
"errorType": "TypeError",
"stackTrace": [
"Object.handle (/var/task/index.js:16:50)",
"GenericHandlerAdapter.<anonymous> (/var/task/node_modules/ask-sdk-runtime/dist/dispatcher/request/handler/GenericHandlerAdapter.js:63:47)",
"step (/var/task/node_modules/ask-sdk-runtime/dist/dispatcher/request/handler/GenericHandlerAdapter.js:44:23)",
"Object.next (/var/task/node_modules/ask-sdk-runtime/dist/dispatcher/request/handler/GenericHandlerAdapter.js:25:53)",
"/var/task/node_modules/ask-sdk-runtime/dist/dispatcher/request/handler/GenericHandlerAdapter.js:19:71",
"new Promise (<anonymous>)",
"__awaiter (/var/task/node_modules/ask-sdk-runtime/dist/dispatcher/request/handler/GenericHandlerAdapter.js:15:12)",
"GenericHandlerAdapter.execute (/var/task/node_modules/ask-sdk-runtime/dist/dispatcher/request/handler/GenericHandlerAdapter.js:61:16)",
"GenericRequestDispatcher.<anonymous> (/var/task/node_modules/ask-sdk-runtime/dist/dispatcher/GenericRequestDispatcher.js:173:58)",
"step (/var/task/node_modules/ask-sdk-runtime/dist/dispatcher/GenericRequestDispatcher.js:44:23)"
]
}

我使用了来自 Alexa SDK 网站的代码,可以在这里找到:https://ask-sdk-for-nodejs.readthedocs.io/en/latest/Calling-Alexa-Service-APIs.html#getinskillproducts .有人可以告诉我出了什么问题或我该如何解决吗?提前致谢。

最佳答案

如果您分享您的导出部分,我可能会提供进一步的帮助。如果您使用带有以下代码的自定义 alexa 技能生成器:

const skillBuilder = Alexa.SkillBuilders.custom();
exports.handler = skillBuilder
.addRequestHandlers()

exports.handler = Alexa.SkillBuilders.custom()
.addRequestHandlers()

那么你只需要添加.withApiClient(new Alexa.DefaultApiClient())

例如,您的导出可能如下所示:

const skillBuilder = Alexa.SkillBuilders.custom();
exports.handler = skillBuilder
.addRequestHandlers(
CancelResponseHandler,
LaunchRequestHandler,
HelloWorldIntentHandler,
HelpIntentHandler,
CancelAndStopIntentHandler,
SessionEndedRequestHandler
)
.addErrorHandlers(ErrorHandler)
.withApiClient(new Alexa.DefaultApiClient())
.lambda();`

我从以下问题中找到了这个解决方案:https://github.com/alexa/alexa-skills-kit-sdk-for-nodejs/issues/356

关于node.js - 无法读取未定义的 Alexa SDK 的属性 'getMonetizationServiceClient',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54595681/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com