存储在Firebase中的对象的值“未定义”

问题描述 投票:0回答:2

我正在尝试在我的firebase数据中接收有关用户的详细信息。我正在使用Ionic和Typescript。

我创建了这样的用户

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

然后检索这样的数据

  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.

但是,当我尝试访问该对象,以便我可以在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"

我已经尝试了很多其他的东西,这些东西在我的代码中没有显示出来。似乎无法获得我想要的数据。

更新:我已经包含了我的数据库的JSON导出,以防万一

{
  "users" : {
    "-L6JNUj7T9wvssjiWjX9" : {
      "email" : "[email protected]",
      "firstName" : "testFirst",
      "institution" : "school-university",
      "isInstructor" : false,
      "lastName" : "testLast"
    }
  }
}

更新:尝试使用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)
  })
typescript firebase ionic-framework firebase-realtime-database
2个回答
0
投票

复制粘贴这个。这段代码肯定会起作用。

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;
});

0
投票

我相信我的真正问题不是检查.push生成的密钥。我正在挖掘JSON对象,但跳过路径中的键,这导致我的所有值都说“未定义”。

所以我们决定改变用户的注册方式。我们决定使用用户的电子邮件作为密钥,而不是使用.push并生成密钥。 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

新的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);
});
}
© www.soinside.com 2019 - 2024. All rights reserved.