0

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

Angie
  • 1

2 Answers2

0

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

instead of

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

try

fetchAndActivate(remoteConfig).then(() => {
  // code
});
James
  • 20,957
  • 5
  • 26
  • 41
  • How do I get the contract_token variable out of that block? I need to use that variable to print it on this label: – Angie Feb 17 '22 at 21:45
  • Does my example work to solve your initial question? – James Feb 17 '22 at 21:50
  • It could work if I didn't need to store a value in the contract_token variable inside the block, but I need to use that variable in a tag outside (the one I mentioned in the previous comment). – Angie Feb 17 '22 at 22:02
  • If you aren't able to use the value inside the .then function, and really need to return it, consider using async/await. https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call – James Feb 17 '22 at 22:08
  • I think I understand that the async/await functions also do not allow to return the value of the variable. – Angie Feb 18 '22 at 00:07
0

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()
Fang Lai
  • 129
  • 3