Reputation: 45
im trying to make a call to firebase to retrieve an user by the Id which is stored in my context.
const obj = firebase.database().ref("/users/" + userContext.state.userId).once('value')
But when i console.log(obj) it returns a Promise instead of an object
Promise {
"_40": 0,
"_55": null,
"_65": 0,
"_72": null,
}
Edit 1 : I have tried this
const obj = database.ref("/users/" + userContext.state.user).once('value').then(snapshot => console.log('User data:', snapshot.val()))
But it prints : User data: null
Upvotes: 1
Views: 160
Reputation: 1060
Here is the code
var user;
firebase.database().ref("/users/" + userContext.state.userId).once('value').then(function(snapshot) {
user= snapshot.val();
});
Hope will work according to your need
in case you need to use the value in state
export default class App extends Component {
constructor() {
super();
this.state = {
User: '',
YourOtherData :'',
YourOtherData2 :'',
};
}
getUserData(){
firebase.database().ref("/users/" + userContext.state.userId).once('value').then(function(snapshot) {
const userData = snapshot.val();
this.setState({ User: userData });
});
}
}
Upvotes: 2
Reputation: 45
Try this:
const obj = firebase.database().ref("/users/" + userContext.state.userId).once('value', function (snapshot))
If you console.log(snapshot.val()) this is what you should get:
Object {
"avatar": "test avatar",
"email": "[email protected]",
"name": "Enter name",
"username": "Enter username",
}
What do you want to do with the userID?
Upvotes: 0