Sunday, May 12, 2024
83
rated 0 times [  88] [ 5]  / answers: 1 / hits: 39411  / 6 Years ago, thu, june 21, 2018, 12:00:00

I have a value stored in a variable called myStation. Now I'd like to find that value in an array located in another file called station.js. When I find a match I would like to grab the stationID. The code I'm using let stationNewName = Stations.find((s) => s.stationName === myStation); is causing the error Error handled: Stations.find is not a function. What am I missing?



I was hoping to not have to load the overhead of Lodash library, and thought I should be able to accomplish with basic javascript code. Here are excerpts from my code that relate to the error:



Requires the station.js file



const Stations = require(./stations.js);


Here's an excerpt leading up to the code causing the error.
The next line is executed in one of my Handlers where myStation is receiving the value CBS



const myStation = handlerInput.requestEnvelope.request.intent.slots.stationName.value;


The next line is producing the error: Error handled: Stations.find is not a function.



let stationNewName = Stations.find((s) => s.stationName === myStation);


This is an excerpt from my array in the stations.js file



STATIONS: [          
{stationName: CBS, stationID: 8532885},
{stationName: NBC, stationID: 8533935},
{stationName: ABC, stationID: 8534048},
],


Updated Array to include full module



'use strict';

module.exports = {

STATIONS: [
{stationName: CBS, stationID: 8532885},
{stationName: NBC, stationID: 8533935},
{stationName: ABC, stationID: 8534048},
],
};

More From » alexa-skills-kit

 Answers
13

Your export contains an object with one property that contains an array. So you need to reference that one property of the object to get to the array that you think you are referencing



let stationNewName = Stations.STATIONS.find((s) => s.stationName === myStation);

[#54148] Tuesday, June 19, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mackennamelissac

Total Points: 110
Total Questions: 118
Total Answers: 103

Location: Sweden
Member since Sun, Jan 16, 2022
2 Years ago
;