我想存储和使用从函数外部读取数据库数据获得的快照值。我试图将其存储为一个值,但在控制台中获取一个未定义的
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/>;
}
}
预计会打印出我当前未定义的用户信息