I'm trying to receive details about a user in my firebase data. I'm using Ionic and Typescript.
我正在尝试在firebase数据中接收有关用户的详细信息。我正在使用Ionic和Typescript。
I create a user like this
我创建了这样的用户
addToDatabase(user: User) {
let isInstructor = user.isInstructor == null ? false : user.isInstructor;
this.afDB.list("/users/").push(
{
"firstName": user.firstName,
"lastName": user.lastName,
"email": user.email,
"institution" : user.institution,
"isInstructor": isInstructor
}
);
}
The user is created as expected
用户按预期创建
Then retrieve the data like this
然后检索这样的数据
async signIn(user: User) {
this.afAuth.auth.signInWithEmailAndPassword(user.email, user.password).then((result) => {
var ref = this.afDB.database.ref("users/");
//retrieve values for a single user by email
ref.orderByChild("email").equalTo(user.email).once("value").then(function (snapshot) {
console.log(snapshot.val());
var value = snapshot.val();
console.log(value);
var firstName = value.firstName;
console.log(value.firstName);
var lastName = JSON.stringify(value.lastName);
console.log(lastName);
//user.firstName = value.firstName; I want to save user details here
//user.lastName = value.lastName; and here for later
}, function (errorObject) {
console.log("The read failed: " + errorObject.code);
});
this.shareService.setUserDetails(user); //user detail is shared in the app
this.navCtrl.setRoot(HomePage);
}).catch(function (error) {
alert("Sign in failed:\n" + error.message);
});
}
This part seems to work fine, I can see the user object in chrome.
这部分似乎工作正常,我可以在chrome中看到用户对象。
But, when I try to access the object so I can show it in the html later It almost always prints on the page as "undefined"
但是,当我尝试访问该对象,以便我可以在html中显示它几乎总是在页面上打印为“undefined”
var value = snapshot.val();
console.log(value); //this works fine, shows the object as expected
console.log(value.firstName); //this is what i want! but it's "undefined"
var lastName = JSON.stringify(value.lastName); //trying to stringify
console.log(lastName); //still "undefined"
I've tried a lot of other things that's not in displayed in my code here. Just cannot seem to get the data I want.
我已经尝试了很多其他的东西,这些东西在我的代码中没有显示出来。似乎无法获得我想要的数据。
Update: I've included the JSON export of my DB in case that helps
更新:我已经包含了我的数据库的JSON导出,以防万一
{
"users" : {
"-L6JNUj7T9wvssjiWjX9" : {
"email" : "test@test.com",
"firstName" : "testFirst",
"institution" : "school-university",
"isInstructor" : false,
"lastName" : "testLast"
}
}
}
Update: Trying to work with promises, this seems to just come back null too.
更新:尝试使用promises,这似乎也回来了null。
public dataTest(user: User) {
var ref = this.afDB.database.ref("users/");
return new Promise(function (resolve, reject) {
try {
ref.once('value', function (snapshot) {
if (typeof snapshot.val().firstName === 'undefined') {
resolve(null);
} else {
console.log(snapshot.val().firstName);
resolve(snapshot.val().firstName);
}
})
} catch (e) {
reject(e)
}
});
}
this.dataTest(user).then(function (result) {
console.log("result " + result) //this just comes back null
}).catch(function (error) {
console.log(error)
})
2 个解决方案
#1
0
Copy paste this. This code will work for sure.
复制粘贴这个。这段代码肯定会起作用。
var ref = this.afDB.database.ref("users/");
ref.orderByChild("email").equalTo(user.email).once("value", (items : any) =>
{
console.log("Key: ",items.key);
console.log("Value: ",items.val());
let users : any = [];
items.forEach((item) =>
{
users.push({
key : item.key,
firstName : item.val().firstName,
lastName : item.val().lastName,
email : item.val().email,
isInstructor : item.val().isInstructor
});
});
console.log("All Users : ",users);
this.userData = users;
});
#2
0
I believe my real issue was not checking for the key that .push generates. I was digging right into the JSON object but skipping the key in the path which what was causing all my values to say "undefined".
我相信我的真正问题不是检查.push生成的密钥。我正在挖掘JSON对象,但跳过路径中的键,这导致我的所有值都说“未定义”。
So we decided to change how our users are registered. Instead of using .push and generating a key we decided to use the user's email as our key. Firebase doesn't like certain characters being put into keys so we have a small bit of code that removes those special characters and stores the key and then we compare against that to identify specific user data. Probably not the most elegant design but we're students and just trying to figure things out.
所以我们决定改变用户的注册方式。我们决定使用用户的电子邮件作为密钥,而不是使用.push并生成密钥。 Firebase不喜欢将某些字符放入密钥中,因此我们只需要一小段代码来删除这些特殊字符并存储密钥,然后我们将其与之进行比较以识别特定的用户数据。可能不是最优雅的设计,但我们是学生,只是想弄清楚事情。
The new function to add user data to Firebase:
将用户数据添加到Firebase的新功能:
addToDatabase(user: User) {
if (!user.isInstructor) {
this.afDB.list("users/students/").set(
user.email
// Remove disallowed characters from key
.replace('.', '')
.replace('#', '')
.replace('$', '')
.replace('[', '')
.replace(']', ''),
{
"firstName": user.firstName,
"lastName": user.lastName,
"email": user.email,
"institution" : user.institution,
"isInstructor": user.isInstructor
}
);
} //more repetitive code if a user is a instructor below this
The new signIn function that find's a user's data, identifies who they are and then retrieves their data, stores it in a user object and sends it to our share service where the whole app can use the data.
新的signIn函数找到用户的数据,识别他们是谁,然后检索他们的数据,将其存储在用户对象中,并将其发送到我们的共享服务,整个应用程序可以使用该数据。
async signIn(user: User) {
this.afAuth.auth.signInWithEmailAndPassword(user.email, user.password).then((result) => {
var usersRef = this.afDB.database.ref("users/");
// Find user in database
this.afDB.database.ref("users/students").child(
user.email
.replace('.', '')
.replace('#', '')
.replace('$', '')
.replace('[', '')
.replace(']', ''))
.once("value", function(snapshot) {
// Get all user info
user.firstName = snapshot.child("firstName").val();
user.lastName = snapshot.child("lastName").val();
user.email = snapshot.child("email").val();
}).then((result) => {
this.shareService.setUserDetails(user);
this.navCtrl.setRoot(HomePage);
});
}).catch(function (error) {
alert("Sign in failed:\n" + error.message);
});
}
#1
0
Copy paste this. This code will work for sure.
复制粘贴这个。这段代码肯定会起作用。
var ref = this.afDB.database.ref("users/");
ref.orderByChild("email").equalTo(user.email).once("value", (items : any) =>
{
console.log("Key: ",items.key);
console.log("Value: ",items.val());
let users : any = [];
items.forEach((item) =>
{
users.push({
key : item.key,
firstName : item.val().firstName,
lastName : item.val().lastName,
email : item.val().email,
isInstructor : item.val().isInstructor
});
});
console.log("All Users : ",users);
this.userData = users;
});
#2
0
I believe my real issue was not checking for the key that .push generates. I was digging right into the JSON object but skipping the key in the path which what was causing all my values to say "undefined".
我相信我的真正问题不是检查.push生成的密钥。我正在挖掘JSON对象,但跳过路径中的键,这导致我的所有值都说“未定义”。
So we decided to change how our users are registered. Instead of using .push and generating a key we decided to use the user's email as our key. Firebase doesn't like certain characters being put into keys so we have a small bit of code that removes those special characters and stores the key and then we compare against that to identify specific user data. Probably not the most elegant design but we're students and just trying to figure things out.
所以我们决定改变用户的注册方式。我们决定使用用户的电子邮件作为密钥,而不是使用.push并生成密钥。 Firebase不喜欢将某些字符放入密钥中,因此我们只需要一小段代码来删除这些特殊字符并存储密钥,然后我们将其与之进行比较以识别特定的用户数据。可能不是最优雅的设计,但我们是学生,只是想弄清楚事情。
The new function to add user data to Firebase:
将用户数据添加到Firebase的新功能:
addToDatabase(user: User) {
if (!user.isInstructor) {
this.afDB.list("users/students/").set(
user.email
// Remove disallowed characters from key
.replace('.', '')
.replace('#', '')
.replace('$', '')
.replace('[', '')
.replace(']', ''),
{
"firstName": user.firstName,
"lastName": user.lastName,
"email": user.email,
"institution" : user.institution,
"isInstructor": user.isInstructor
}
);
} //more repetitive code if a user is a instructor below this
The new signIn function that find's a user's data, identifies who they are and then retrieves their data, stores it in a user object and sends it to our share service where the whole app can use the data.
新的signIn函数找到用户的数据,识别他们是谁,然后检索他们的数据,将其存储在用户对象中,并将其发送到我们的共享服务,整个应用程序可以使用该数据。
async signIn(user: User) {
this.afAuth.auth.signInWithEmailAndPassword(user.email, user.password).then((result) => {
var usersRef = this.afDB.database.ref("users/");
// Find user in database
this.afDB.database.ref("users/students").child(
user.email
.replace('.', '')
.replace('#', '')
.replace('$', '')
.replace('[', '')
.replace(']', ''))
.once("value", function(snapshot) {
// Get all user info
user.firstName = snapshot.child("firstName").val();
user.lastName = snapshot.child("lastName").val();
user.email = snapshot.child("email").val();
}).then((result) => {
this.shareService.setUserDetails(user);
this.navCtrl.setRoot(HomePage);
});
}).catch(function (error) {
alert("Sign in failed:\n" + error.message);
});
}