【问题标题】:How to add data in Firebase Database with unique keys and then fetch the data with unique keys?如何使用唯一键在 Firebase 数据库中添加数据,然后使用唯一键获取数据?
【发布时间】:2019-05-25 05:34:38
【问题描述】:

-LUeq8x-5QaVSXKJdXtz 是关键

signup = async () => {
    const { email, pass, job, description, name, age } = this.state;
// fetching state variables to use in this function


    try {   
      await firebase.auth().createUserWithEmailAndPassword(email, pass);

      var playersRef = firebase.database().ref("doctorsList/");

//push function adds the data with a unique key
      playersRef.push({
        doctor: {
          name: name,
          age: age,
          job: job,
          description: description
      },
      });
}
catch (error) { console.log(error)  }
}

如果 Firebase 数据库具有唯一键,我如何从它们获取数据?

var ref = firebase.database().ref( 'doctorsList/doctor/' );

  ref.on('value', (snapshot) => {
    alert(snapshot.val().name);
 });

当我使用alert(snapshot.val().name); 时,它会显示数据库中的名称,但是当我尝试通过this.setState({ name: snapshot.val().name }); 保存数据时,它不会保存。或者我怎样才能一次从数据库中保存数据?

【问题讨论】:

    标签: javascript firebase react-native firebase-realtime-database


    【解决方案1】:

    Tony 的回答显示了一旦您知道了医生的钥匙,如何联系他们。还有很多场景你可能不知道密钥。

    例如,如果您对要阅读的医生一无所知,您所能做的就是阅读所有医生的信息:

    var ref = firebase.database().ref( 'doctorsList' );
    
    ref.on('value', (snapshot) => {
      snapshot.forEach((doctorSnapshot) => {
        alert(doctorSnapshot.key+": "+doctorSnapshot.val().doctor.name);
      });
    });
    

    例如,如果您知道医生的姓名,则可以使用查询来检索与该姓名匹配的医生:

    ref.orderByChild("doctor/name").equalTo("shubham").on('value', (snapshot) => {
      snapshot.forEach((doctorSnapshot) => {
        alert(doctorSnapshot.key);
      });
    });
    

    您仍然需要在这里snapshot.forEach,因为可能有多个名称匹配的医生。

    如果您想更改每个医生的某些内容,可以在回调中更新:

    ref.orderByChild("doctor/name").equalTo("shubham").on('value', (snapshot) => {
      snapshot.forEach((doctorSnapshot) => {
        doctorSnapshot.ref.child("job").set("doctor");
      });
    });
    

    【讨论】:

    • 谢谢弗兰克。但是我无法将通过 snapshot.val().name 获取的值保存在状态变量中。例如:我无法通过 this.setState({ name: snapshot.val() }); 保存它
    • 这里的“我不能”是什么意思?有错误信息吗?它不会被渲染吗?请简洁准确。
    • 我正在这样做 -- this.setState({ name: snapshot.val().name }) // 这不会更新名称。我想保存从数据库中获取的数据但是如果我只使用 ---- alert(snapshot.val().name); // 这显示了我的名字
    • 请帮忙!我很困惑如何保存从 firebase 数据库中获取的数据
    • 您在该州设置的数据是如何使用的?您共享的代码并没有显示出这一点,因此很难比托尼和我已经完成的工作提供更多帮助。我强烈推荐阅读how to create a minimal, complete, verifiable example,了解如何创建问题的独立重现,这反过来又使我们更有可能提供帮助。
    【解决方案2】:

    如果要使用特定密钥上传,请将 ref 更改为:

    const key = //your key here;
    var playersRef = firebase.database().ref(`doctorsList/${key}`);
    

    然后如果你想用特定的键获取数据,改成这样:

    var ref = firebase.database().ref( `doctorsList/${key}`);
    
      ref.on('value', (snapshot) => {
        alert(snapshot.val().name);
     });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-18
      • 2021-02-28
      • 1970-01-01
      • 1970-01-01
      • 2020-04-14
      • 1970-01-01
      • 2018-06-01
      • 1970-01-01
      相关资源
      最近更新 更多