Object shown as empty but has properties in react native

80 views Asked by At

I have a weird case where my response from Firebase is shown in a object, but when I want to extract the keys, they are 0.

For example

const answersFromDB = FirebaseHelp.getAnsweredQuestions(user.uid);
console.log(answersFromDB); // returns a {}

When I expand the object I see the wanted properties as arrays.

console.log(Object.keys(answersFromDB)); // returns empty array

But, after I set it to the state with the hook and access it from the state, the data is as it should be.

const [answers, setAnswers] = useState([]);

const answersFromDB = FirebaseHelp.getAnsweredQuestions(user.uid);

setAnswers(answersFromDB)

console.log(Object.keys(answers)); // returns a ['key1', 'key2']

Did anybody had the same issues or knows why it is behaving like that?

1

There are 1 answers

0
Davor Marić On

After some investigating I have seen that I have the small "i" icon in the chrome console for "value below was evaluated just now". So probably a problem with the timing while accessing the data.

The solution was to make a async function and await the complete response