JavaScript:当所有类型的对象都相同时,按对象类型访问数组中的哈希

JavaScript:当所有类型的对象都相同时,按对象类型访问数组中的哈希,javascript,botframework,azure-language-understanding,Javascript,Botframework,Azure Language Understanding,我有一个叫做识别器的数组 看起来是这样的: RECOGNIZERS [ LuisRecognizer { _onEnabled: [], _onFilter: [], models: { '*': 'https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/CENSOR?subscription-key=CENSOR&verbose=true&timezoneOffset=0&q=

我有一个叫做识别器的数组

看起来是这样的:

RECOGNIZERS
[ LuisRecognizer {
    _onEnabled: [],
    _onFilter: [],
    models:
     { '*': 'https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/CENSOR?subscription-key=CENSOR&verbose=true&timezoneOffset=0&q=' } },
  LuisRecognizer {
    _onEnabled: [],
    _onFilter: [],
    models:
     { '*': 'https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/CENSOR?subscription-key=CENSOR&verbose=true&timezoneOffset=0&q=' } },
  RegExpRecognizer {
    _onEnabled: [],
    _onFilter: [],
    intent: '/^(help|demo|\\?)$/i',
    expressions: { '*': /^(help|demo|\?)$/i } },
  RegExpRecognizer {
    _onEnabled: [],
    _onFilter: [],
    intent: '/^env$/i',
    expressions: { '*': /^env$/i } },
  RegExpRecognizer {
    _onEnabled: [],
    _onFilter: [],
    intent: '/^luis_test$/i',
    expressions: { '*': /^luis_test$/i } },
  RegExpRecognizer {
    _onEnabled: [],
    _onFilter: [],
    intent: '/^getLocationId/i',
    expressions: { '*': /^getLocationId/i } },
  RegExpRecognizer {
    _onEnabled: [],
    _onFilter: [],
    intent: '/^setLocationId/i',
    expressions: { '*': /^setLocationId/i } } ]
if (typeof RECOGNIZERS[i].models !== 'undefined') {
builder.LuisRecognizer.recognize(args.response, RECOGNIZERS[i].models['*'],
                function (err, intents, entities) {
                    results.push({ response: {entities: entities, intents: intents} });
                    // session.endDialogWithResult({ response: {entities: entities, intents: intents} });
                });
我正在运行for循环,以收集每个
LuisRecognizer
模型,并将其附加到我稍后要访问的数组中。在运行该循环时,我像这样调用我的模型:
RECOGNIZERS[I].models['*]
。但是,一旦它命中识别器数组中的RegExpRecognizer值,就会出错

我尝试添加下面的if语句来尝试忽略非模型。 该代码如下所示:

RECOGNIZERS
[ LuisRecognizer {
    _onEnabled: [],
    _onFilter: [],
    models:
     { '*': 'https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/CENSOR?subscription-key=CENSOR&verbose=true&timezoneOffset=0&q=' } },
  LuisRecognizer {
    _onEnabled: [],
    _onFilter: [],
    models:
     { '*': 'https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/CENSOR?subscription-key=CENSOR&verbose=true&timezoneOffset=0&q=' } },
  RegExpRecognizer {
    _onEnabled: [],
    _onFilter: [],
    intent: '/^(help|demo|\\?)$/i',
    expressions: { '*': /^(help|demo|\?)$/i } },
  RegExpRecognizer {
    _onEnabled: [],
    _onFilter: [],
    intent: '/^env$/i',
    expressions: { '*': /^env$/i } },
  RegExpRecognizer {
    _onEnabled: [],
    _onFilter: [],
    intent: '/^luis_test$/i',
    expressions: { '*': /^luis_test$/i } },
  RegExpRecognizer {
    _onEnabled: [],
    _onFilter: [],
    intent: '/^getLocationId/i',
    expressions: { '*': /^getLocationId/i } },
  RegExpRecognizer {
    _onEnabled: [],
    _onFilter: [],
    intent: '/^setLocationId/i',
    expressions: { '*': /^setLocationId/i } } ]
if (typeof RECOGNIZERS[i].models !== 'undefined') {
builder.LuisRecognizer.recognize(args.response, RECOGNIZERS[i].models['*'],
                function (err, intents, entities) {
                    results.push({ response: {entities: entities, intents: intents} });
                    // session.endDialogWithResult({ response: {entities: entities, intents: intents} });
                });
但即使使用该逻辑检查,它仍然会出错,因为它似乎覆盖了if语句,并且仍然尝试读取RegExpRecognizer值。以下是我的控制台日志:

TypeError: Cannot read property 'models' of undefined
    at Array.GetLuisModel (/Users/sam/lifeai/therealdeal/bots/location-assistant-bot/dialog.js:354:39)
    at /Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/lib/dialogs/WaterfallDialog.js:67:39
    at next (/Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/lib/dialogs/WaterfallDialog.js:92:21)
    at WaterfallDialog.beforeStep (/Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/lib/dialogs/WaterfallDialog.js:99:9)
    at WaterfallDialog.doStep (/Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/lib/dialogs/WaterfallDialog.js:61:14)
    at WaterfallDialog.dialogResumed (/Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/lib/dialogs/WaterfallDialog.js:46:14)
    at Session.endDialogWithResult (/Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/lib/Session.js:358:28)
    at PromptText.Prompt.invokeIntent (/Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/lib/dialogs/Prompt.js:331:21)
    at PromptText.Prompt.replyReceived (/Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/lib/dialogs/Prompt.js:147:18)
    at Session.routeToActiveDialog (/Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/lib/Session.js:525:24)
    at Library.selectActiveDialogRoute (/Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/lib/bots/Library.js:150:17)
    at Library.defaultSelectRoute (/Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/lib/bots/Library.js:301:22)
    at Library.selectRoute (/Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/lib/bots/Library.js:96:18)
    at disambiguateRoute (/Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/lib/bots/UniversalBot.js:398:62)
    at /Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/lib/bots/UniversalBot.js:407:21
    at /Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/node_modules/async/lib/async.js:52:16
    at Object.async.forEachOf.async.eachOf (/Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/node_modules/async/lib/async.js:236:30)
    at Object.async.forEach.async.each (/Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/node_modules/async/lib/async.js:209:22)
    at /Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/lib/bots/UniversalBot.js:386:19
    at /Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/lib/bots/Library.js:64:17
    at /Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/lib/dialogs/IntentRecognizer.js:23:25
    at /Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/lib/dialogs/IntentRecognizerSet.js:84:17
    at /Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/node_modules/async/lib/async.js:52:16
    at /Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/node_modules/async/lib/async.js:322:29
    at /Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/node_modules/async/lib/async.js:44:16
    at /Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/lib/dialogs/IntentRecognizerSet.js:76:17
    at /Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/node_modules/async/lib/async.js:181:20
    at replenish (/Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/node_modules/async/lib/async.js:319:21)
    at /Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/node_modules/async/lib/async.js:330:15
    at Object.async.forEachLimit.async.eachLimit (/Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/node_modules/async/lib/async.js:220:35)
    at IntentRecognizerSet.recognizeInParallel (/Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/lib/dialogs/IntentRecognizerSet.js:66:15)
    at IntentRecognizerSet.onRecognize (/Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/lib/dialogs/IntentRecognizerSet.js:52:18)
    at /Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/lib/dialogs/IntentRecognizer.js:18:23
    at next (/Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/lib/dialogs/IntentRecognizer.js:50:17)
    at IntentRecognizerSet.IntentRecognizer.isEnabled (/Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/lib/dialogs/IntentRecognizer.js:53:9)
    at IntentRecognizerSet.IntentRecognizer.recognize (/Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/lib/dialogs/IntentRecognizer.js:10:14)
    at UniversalBot.Library.recognize (/Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/lib/bots/Library.js:59:30)
    at UniversalBot.routeMessage (/Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/lib/bots/UniversalBot.js:374:14)
    at /Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/lib/bots/UniversalBot.js:291:23
    at /Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/lib/bots/UniversalBot.js:352:78
    at next (/Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/lib/Session.js:88:17)
    at /Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/lib/Session.js:107:17
    at /Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/lib/DefaultLocalizer.js:64:17
    at /Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/node_modules/async/lib/async.js:52:16
    at done (/Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/node_modules/async/lib/async.js:246:17)
    at /Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/node_modules/async/lib/async.js:44:16
    at /Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/lib/DefaultLocalizer.js:61:64
    at tryCallOne (/Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/node_modules/promise/lib/core.js:37:12)
    at /Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/node_modules/promise/lib/core.js:123:15
    at flush (/Users/sam/lifeai/therealdeal/bots/location-assistant-bot/node_modules/botbuilder/node_modules/asap/raw.js:50:29)
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickDomainCallback (internal/process/next_tick.js:218:9)

.getLuisModel - ERROR: Cannot read property 'models' of undefined
.getLuisModel - Session.endConversation()
Session.sendBatch() sending 2 message(s)
您的错误是:

无法读取未定义的属性“models”

这意味着
识别器[i]
未定义。如果试图访问数组中超出范围的元素,或者数组中有未定义的元素,则可以将If语句更改为:

if (typeof (RECOGNIZERS[i]||{}).models !== 'undefined') {
或者使用过滤器正确收集模型:

models = RECOGNIZERS.filter(
  function(item){return !!item.models;}
)
您的错误是:

无法读取未定义的属性“models”

这意味着
识别器[i]
未定义。如果试图访问数组中超出范围的元素,或者数组中有未定义的元素,则可以将If语句更改为:

if (typeof (RECOGNIZERS[i]||{}).models !== 'undefined') {
或者使用过滤器正确收集模型:

models = RECOGNIZERS.filter(
  function(item){return !!item.models;}
)

我有一个怀疑可能有关联。我有一个怀疑可能有关联。stacktrace相当大…stacktrace相当大。。。