-2

I have a function that return a undefined thing even if it normally need to return something defined.

I have this code with a function:

const GtfsRealtimeBindings = require("gtfs-realtime-bindings");
const fetch = require("node-fetch");
const long = require("long");

async function getTGVStops(tgv_number){

    var answer

    try {
      const response = await fetch("https://proxy.transport.data.gouv.fr/resource/sncf-tgv-gtfs-rt-trip-updates", {
        headers: {
          //"x-api-key": "<redacted>",
          // replace with your GTFS-realtime source's auth token
          // e.g. x-api-key is the header value used for NY's MTA GTFS APIs
        },
      });
      if (!response.ok) {
        const error = new Error(`${response.url}: ${response.status} ${response.statusText}`);
        error.response = response;
        throw error;
        process.exit(1);
      }
      const buffer = await response.arrayBuffer();
      const feed = GtfsRealtimeBindings.transit_realtime.FeedMessage.decode(
        new Uint8Array(buffer)
      );
      feed.entity.forEach((entity) => {
        if (entity.id.includes(tgv_number)) {
          answser = entity.tripUpdate.stopTimeUpdate
          console.log(answser)
          return answser
        }
      })
    }
    catch (error) {
      console.log(error);
      process.exit(1);
    }
}

module.exports = { getTGVStops }

and want to call it in another files,

const tgv = require("./tgv_information.js")
const station = require("./station.js")
const GtfsRealtimeBindings = require("gtfs-realtime-bindings");

tgv.getTGVStops("6033").then((answer) => { 
    console.log("test")
    console.log(answer); 
});

But it always returns undefined.

6
  • 3
    there is no return statement in function getTGVStops Commented Aug 18, 2024 at 9:09
  • @JaromandaX I have put one it is in the if, I don't know where to put the return with a try, catch Commented Aug 18, 2024 at 9:12
  • Rewrite the feed.entity.forEach to a normal for loop. The inner function of the forEach means that its return statement applies to that inner function instead of to the outer function. By using a for there will be no inner function. Commented Aug 18, 2024 at 9:12
  • 1
    @YoanPettorelli - that's not a return from function getTGVStops Commented Aug 18, 2024 at 9:15
  • 1
    Notice also, that the return value of the callback of forEach is ignored. Commented Aug 18, 2024 at 9:53

2 Answers 2

1

The return statement you have inside the forEach will return from the closure, not the calling function.

One approach is to use a straightforward for-of loop instead of a forEach call:

for (entity of feed.entity) {
  if (entity.id.includes(tgv_number)) {
    answser = entity.tripUpdate.stopTimeUpdate
    console.log(answser)
    return answser
  }
}
Sign up to request clarification or add additional context in comments.

Comments

-2

Because it was written in the definition: var answer But using in :()

if (entity.id.includes(tgv_number)) {
          answser = entity.tripUpdate.stopTimeUpdate
          console.log(answser)
          return answser
        }

so it will be undefied,because you have not define answser, change "answser" to "answer" it will be fixed

1 Comment

I don't think this is correct as the scope of answer and answser are different.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.