Array.find is not a function error
Asked Answered
B

2

12

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"},
    ],
};
Beauty answered 21/6, 2018 at 16:8 Comment(12)
What is exported from stations.js though?Zoroastrianism
Well Stations is not what you think it is most likely. console.log(Stations)Iridic
@Iridic - console.log(Stations) returns the JSON containing the array above.Beauty
So than your code would have to reference STATIONS if it is in object being returned.Iridic
@Iridic - Like this: let stationNewName = Stations.find((s) => STATIONS[s.stationName === myStation]);?Beauty
no like Stations.STATIONSIridic
@Iridic - Okay, I tried let stationNewName = STATION.Stations.find((s) => s.stationName === myStation); and now I get an error "Error handled: STATION is not defined".Beauty
lol, I did not type STATION.StationsIridic
In the end, how you are doing the export is probably wrong.Iridic
@Iridic - If I was doing the export wrong wouldn't I not get the returned JSON from the console.log(Stations) statement?Beauty
@Iridic - Sorry, just saw your previous one where I transposed STATION.Stations. I try again.Beauty
There is no reason to have the STATIONS in the export if that is the only thing it is going to contain, if you are going to have more in there in the future than you can namespace it. You have an object and you are not referencing the property of it.Iridic
I
9

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);
Iridic answered 21/6, 2018 at 16:43 Comment(1)
That eliminates the one error, but logs "stationNewName = [object Object]". So in part of the original question I ask, "When I find a match I would like to grab the stationID". Do I need an if statement like: if (Stations.STATIONS.find((s) => s.stationName === myStation)) { let stationNewName = s.stationID or Stations.STATIONS.stationID }?Beauty
S
0

After using the find method which will return the element of the array if the passed predicate is true you need to reference the member stationId as each element within your STATIONS array is an object.

'use strict';

module.exports = {
  STATIONS: [{
      stationName: "CBS",
      stationID: "8532885"
    },
    {
      stationName: "NBC",
      stationID: "8533935"
    },
    {
      stationName: "ABC",
      stationID: "8534048"
    },
  ],
};

// Import the default export from the stations.js module which is the object containing the STATIONS array.
const Stations = require("./stations.js");

const myStation = 'STATION_NAME';

// Find the first element within STATIONS with the matching stationName
const station = Stations.STATIONS.find((s) => s.stationName === myStation);

// As find will return the found element which is an object you need to reference the stationID member.
const stationId = station.stationID;
Shorter answered 21/6, 2018 at 16:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.