I've got some data from Firebase Realtime Database and now I'm trying to add it to a variable. I'm sure that the querying works, because if I console.log(dataSnapshot) it logs the correct data (which is 001). However, when I'm trying to create a variable out of that number I get the following error: Cannot read property 'setState' of undefined and it doesn't return anything to the console.
Here's my code:
class Table extends Component {
constructor(props) {
super(props);
this.state = { timestamps: [], user: auth().currentUser, serial: "" };
}
componentDidMount() {
const uid = this.state.user.uid;
console.log(uid);
let serial = this.state.serial;
const serialRef = db.ref(uid + "/serial");
serialRef.once("value").then(function (dataSnapshot) {
console.log(dataSnapshot.val());
this.setState({ serial: dataSnapshot.val() }, () => {
serial = this.state.serial;
console.log(serial);
});
});
Here's the screenshot of my console
Thanks in advance!