0

I would like to store and use the value of a snap from reading data of a database outside the function. I have tried to store it in a value but a get an undefined in the console

import React, {Component} from 'react';
import {
View,
} from 'react-native';
import firebase from 'firebase'
export default class test extends Component {
  constructor() {
  super();
}

componentWillMount() {
 let user;
  firebase.database().ref('/Users/' + firebase.auth().currentUser.uid).on('value', function (snap)  {
   console.log(snap); // good response printed here
   user = snap
})
  console.log(user)
   // use the value if different ways
}


render() {
  return <View/>;
 }
}

expected to get the user information printed I currently get undefined

J.lan
  • 223
  • 4
  • 17
  • Store it in the state. You can't return it. – Quentin Feb 04 '19 at 17:03
  • You do `user = snap` inside of an async callback, but console.log it outside of the callback -- where you're logging it, it ISN'T defined yet. You have to wait for it. – TKoL Feb 04 '19 at 17:04

0 Answers0