如何缩短此功能的使用时间? Alexa / NodeJs

时间:2019-05-24 17:44:14

标签: javascript node.js alexa alexa-skill alexa-slot

我具有用于返回插槽值的此函数。如果广告位是同义词,则会将其修改为标准广告位值。这是我现在拥有的代码:

var slotHelper = function() { };

slotHelper.prototype.slotValue = (slot, useId) => {
    let value = slot.value;
    let resolution = (
        slot.resolutions
        && slot.resolutions.resolutionsPerAuthority
        && slot.resolutions.resolutionsPerAuthority.length > 0
    )
        ? slot.resolutions.resolutionsPerAuthority[0]
        : null;
    if (resolution && resolution.status.code == 'ER_SUCCESS_MATCH') {
        let resolutionValue = resolution.values[0].value;
        value = resolutionValue.id && useId
            ? resolutionValue.id
            : resolutionValue.name;
    }
    return value;
};

//Example
//let category = slotValue(handlerInput.requestEnvelope.request.intent.slots.category, true);

module.exports = new slotHelper();

我试图将// example简化为我可以键入的位置:

let category = slotHelper.slotValue(category);

并返回标准插槽值。

我尝试了一些不同的方法,它们都导致:“错误处理:类别未定义”

以下是我尝试过的不同方法:

var slotHelper = function() { };

slotHelper.prototype.slotValue = (slotName, useId) => {
    let slot = handlerInput.requestEnvelope.request.intent.slots[slotName];
    let value = slot.value;
    let resolution = (
        slot.resolutions
        && slot.resolutions.resolutionsPerAuthority
        && slot.resolutions.resolutionsPerAuthority.length > 0
    )
        ? slot.resolutions.resolutionsPerAuthority[0]
        : null;
    if (resolution && resolution.status.code == 'ER_SUCCESS_MATCH') {
        let resolutionValue = resolution.values[0].value;
        value = resolutionValue.id && useId
            ? resolutionValue.id
            : resolutionValue.name;
    }
    return value;
}

module.exports = new slotHelper();

AND

var slotHelper = function() { };

slotHelper.prototype.slotValue = (slotName, useId) => {
    let slot = handlerInput.requestEnvelope.request.intent.slots.slotName;
    let value = slot.value;
    let resolution = (
        slot.resolutions
        && slot.resolutions.resolutionsPerAuthority
        && slot.resolutions.resolutionsPerAuthority.length > 0
    )
        ? slot.resolutions.resolutionsPerAuthority[0]
        : null;
    if (resolution && resolution.status.code == 'ER_SUCCESS_MATCH') {
        let resolutionValue = resolution.values[0].value;
        value = resolutionValue.id && useId
            ? resolutionValue.id
            : resolutionValue.name;
    }
    return value;
}

module.exports = new slotHelper();

当然,第二个slotName是灰色的,因为这不是正确的方法。有谁知道我如何将其修改为可以使用的地方:

let category = slotHelper.slotValue(category);

我正在尝试推广此功能,以便将来可以在所有插槽中使用它。这样可以更轻松地在我的代码中使用...而不是总是要输入:

let category = slotHelper.slotValue(handlerInput.requestEnvelope.request.intent.slots.category);

3 个答案:

答案 0 :(得分:0)

通过检查resolutionsPerAuthority数组是否存在,一个可能有助于消除很多样板的建议可能是使用lodash's head方法:


const resolution = _.head(slot.resolutions.resolutionsPerAuthority);

_.head可以处理空或空数组输入,因此这应该有助于减少函数的很多认知开销。

答案 1 :(得分:0)

我自己修复了。我不得不说:

let categories = handlerInput.requestEnvelope.request.intent.slots.category;

let category = slotHelper.slotValue(categories);

在我的index.js(主文件)中,而不是在helper函数中,因为index.js使用ask-sdk构建响应。我创建的模块不具备此功能。

答案 2 :(得分:0)

使用this function,因为它是您要找的帮助者