简体   繁体   English

使用Ionion Creator访问Firebase数据时遇到问题

[英]Having trouble accessing firebase data with ionic creator

I'm a novice IOS developer practicing building my first app. 我是IOS开发人员,正在尝试构建我的第一个应用程序。 However, I am unable to access data in firebase. 但是,我无法访问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. 上面打印出“ quanie.turner@gmail.com未定义”,但是在数据库中明确定义了用户名变量。 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) : 方法如下(顺便说一句:scope.data数组包括username变量):

 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: 您似乎混淆了两种产品:

  1. Firebase Authentication, which allows you users to sign in to your app, and which has a user profile with a fixed set of properties. Firebase身份验证,可让您的用户登录您的应用,并具有带有一组固定属性的用户个人资料。
  2. Firebase Database, which allows you to store data, and which is typically used to store additional properties for a user that don't fit into their Firebase Authentication profile. Firebase数据库,它允许您存储数据,通常用于存储不适合其Firebase身份验证配置文件的用户的其他属性。

The code you have reads the user's profile from Firebase Authentication. 您已获取的代码从Firebase身份验证中读取用户的个人资料。 It does not read anything from the Firebase Database. 它不会从Firebase数据库读取任何内容。 A user profile in Firebase Authentication doesn't have a username property. Firebase身份验证中的用户配置文件没有username属性。 It does have a displayName property, but that again has nothing to do with the Firebase Database. 确实具有displayName属性,但这又与Firebase数据库无关。

It looks like you stored the additional user information into the Firebase Database with a call like: 您似乎通过以下调用将其他用户信息存储到了Firebase数据库中:

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. 尽管这是在数据库中存储数据的有效方法,但它使得为Firebase身份验证用户查找数据变得困难。 The latter users are identified by their UID ( user.uid ), which is why it's custom to store additional user properties under /users/$uid : 后面的用户由其UID( user.uid )标识,这就是为什么习惯将其他用户属性存储在/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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM