I'm a novice IOS developer practicing building my first app. However, I am unable to access data in firebase.
For example, I have the following data:
I'll log into the app with the username and password which work fine and then use:
var user = firebase.auth().currentUser;
console.log(user.email + " " + user.username);
The above prints out "quanie.turner@gmail.com undefined" however the username variable is clearly defined in the database. Maybe I'm overlooking something or did something wrong. I'd appreciate any help.
Edit: I stored the data in my signup code. Here's how (btw: the scope.data array includes the username variable) :
function ($scope, $stateParams, $firebaseAuth, $firebaseArray, $state) { var auth = $firebaseAuth(); var ref = firebase.database().ref('users'); var users = $firebaseArray(ref); $scope.data = {}; $scope.signup = function(){ auth.$createUserWithEmailAndPassword($scope.data.email, $scope.data.password).then($scope.signupSuccess); } $scope.signupSuccess = function(){ users.$add($scope.data); $state.go('login'); } }
Thanks
You seem to be confusing two products:
The code you have reads the user's profile from Firebase Authentication. It does not read anything from the Firebase Database. A user profile in Firebase Authentication doesn't have a username
property. It does have a displayName
property, but that again has nothing to do with the Firebase Database.
It looks like you stored the additional user information into the Firebase Database with a call like:
firebase.database().ref("users").push({ email: user.email, password: password, username: username })
While this is a valid way to store data in the database, it makes it hard to look up data for a Firebase Authentication user. The latter users are identified by their UID ( user.uid
), which is why it's custom to store additional user properties under /users/$uid
:
firebase.database().ref("users").child(user.uid).set({ email: user.email, password: password, username: username })
If you have that structure, you can load the additional properties for the current user with:
var usersRef = firebase.database().ref("users");
usersRef.child(user.uid).once("value").then(function(snapshot) {
console.log(snapshot.val());
});
With your current structure you can also look up the user. But since there may be multiple users with the same email address in your structure, you need a query for it and need to handle the fact that the query may match multiple nodes:
var usersRef = firebase.database().ref("users");
var query = usersRef.orderByChild("email").equalTo(user.email);
query.once("value").then(function(snapshot) {
snapshot.forEach(function(userSnapshot) {
console.log(userSnapshot.val());
});
});
Update
As far as I can see you should replace your
users.$add($scope.data);
With
ref.child(firebase.auth().currentUser.uid).set($scope.data);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.