Array.find 不是函数错误

Hac*_*rew 4 javascript alexa-skills-kit

我有一个值存储在名为 myStation 的变量中。现在我想在位于另一个名为 station.js 的文件中的数组中找到该值。当我找到匹配项时,我想获取 stationID。我使用的代码let stationNewName = Stations.find((s) => s.stationName === myStation);导致错误“错误处理:Stations.find 不是函数”。我错过了什么?

我希望不必加载 Lodash 库的开销,并认为我应该能够使用基本的 javascript 代码来完成。以下是我的代码中与错误相关的摘录:

需要 station.js 文件

const Stations = require("./stations.js");
Run Code Online (Sandbox Code Playgroud)

这是导致错误的代码的摘录。下一行在我的一个处理程序中执行,其中 myStation 正在接收值“CBS”

const myStation = handlerInput.requestEnvelope.request.intent.slots.stationName.value;
Run Code Online (Sandbox Code Playgroud)

下一行产生错误:“错误处理:Stations.find 不是函数”。

let stationNewName = Stations.find((s) => s.stationName === myStation);
Run Code Online (Sandbox Code Playgroud)

这是我在stations.js 文件中的数组的摘录

STATIONS: [          
          {stationName: "CBS", stationID: "8532885"},
          {stationName: "NBC", stationID: "8533935"},
          {stationName: "ABC", stationID: "8534048"},
    ],  
Run Code Online (Sandbox Code Playgroud)

更新 Array 以包含完整模块

'use strict';

module.exports = {

STATIONS: [          
          {stationName: "CBS", stationID: "8532885"},
          {stationName: "NBC", stationID: "8533935"},
          {stationName: "ABC", stationID: "8534048"},
    ],
};
Run Code Online (Sandbox Code Playgroud)

epa*_*llo 8

您的导出包含一个对象,该对象具有一个包含数组的属性。所以你需要引用对象的一个​​属性来访问你认为引用的数组

let stationNewName = Stations.STATIONS.find((s) => s.stationName === myStation);
Run Code Online (Sandbox Code Playgroud)