'Firebase - remote config on web sometimes gets values and sometimes not

I'm new to programming in Javascript (I haven't even been programming for a month, but I have experience in other non-asynchronous languages).

I'm having trouble connecting the Firebase config to my website, sometimes it shows values, other times it doesn't.

if (app_data) {
    console.log("Step 0");
    remoteConfig = getRemoteConfig(app_data);
    remoteConfig.settings.minimumFetchIntervalMillis = 10000;
  } else {
    console.log("No se ha podido conectar a la base de datos.");
  }

  if (fetchAndActivate(remoteConfig)) {
    console.log("Paso por 2");
    obj_config = getAll(remoteConfig);
    console.log("Paso por 3");
    Object.entries(obj_config).forEach((x) => {
      const [key, entry] = x;
      if (key === "contract_address") {
        contract_token = entry["_value"];
        console.log("Key Address: ", entry["_value"]);
      }
      console.log("Key: ", key);
      console.log("Source: ", entry.getSource());
      console.log("Value: ", JSON.stringify(entry["_value"]));
    });
    
  }

If someone could help me, I would be very grateful.

Greetings



Solution 1:[1]

fetchAndActivate is async, so it won't have finished fetching yet:

instead of

if (fetchAndActivate(remoteConfig)) {
  //code
}

try

fetchAndActivate(remoteConfig).then(() => {
  // code
});

Solution 2:[2]

If you are using Javascript, you could try to use await in your code because the fetching step is asynchronous. Something like

const rcResult = await asyncFetchFromRC()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 James
Solution 2 Fang Lai