如何使用JavaScript中的on()查询Firebase实时数据库?

huangapple go评论70阅读模式
英文:

How to query Firebase Realtime Database using on() in JavaScript?

问题

我试图使用JavaScript查询Firebase实时数据库,以根据其电子邮件检索特定用户。我想在我的查询中同时使用equalTo和orderByChild方法,但我遇到了问题。以下是我有的代码片段:

const usersRef = ref(database, 'users');
const userEmail = "testemail123@gmail.com";

const query = query(usersRef, orderByChild('email'), equalTo(userEmail));

query.on('value', (snapshot) => {
  if (snapshot.exists()) {
    // 找到了具有电子邮件地址`userEmail`的用户。
    const user = snapshot.val();
    console.log(user)
  } else {
    // 找不到具有电子邮件地址`userEmail`的用户。
  }
});

然而,当我运行这段代码时,我收到一个错误,指示emailQuery.on未定义。似乎我不能直接在查询函数内部使用on()。

登录错误:emailQuery.on不是一个函数

我如何定义"on()"?我如何修改我的代码以实现所需的功能?

我已经尝试了其他函数,如once或forEach,但它们似乎提供了相同的错误。

我还尝试了这段代码:

const usersRef = ref(database, 'users');
console.log(usersRef);

usersRef.orderByChild('email').equalTo(userEmail)
  .once('value').then((snapshot) => {
    const userObject = snapshot.val();
    console.log(userObject);
  })

但它只为我提供了以下错误:

登录错误:usersRef.orderByChild不是一个函数

英文:

I'm trying to query a Firebase Realtime Database to retrieve a specific user based on their email using JavaScript. I want to use the equalTo and orderByChild methods together in my query, but I'm encountering issues. Here's the code snippet I have:

const usersRef = ref(database, 'users');
const userEmail = "testemail123@gmail.com";

const query = query(usersRef, orderByChild('email'), equalTo(userEmail));

emailQuery.on('value', (snapshot) => {
        if (snapshot.exists()) {
          // The user with the email address `userEmail` was found.
          const user = snapshot.val();
          console.log(user)
        } else {
          // The user with the email address `userEmail` was not found.
        }
      });

However, when I run this code, I get an error indicating that emailQuery.on is not defined. It seems that I cannot use on() directly within the query function.

> Sign-in error: emailQuery.on is not a function

How can I define "on()"? How can I modify my code to achieve my desired functionality?

I already tried other functions like once or forEach but they seem to provide the same error.

I've also tried this code:

      const usersRef = ref(database, 'users');
      console.log(usersRef);

      usersRef.orderByChild('email').equalTo(userEmail)
      .once('value').then((snapshot) => {
        const userObject = snapshot.val();
        console.log(userObject);
      })

but it just provides me this error:

> Sign-in error: usersRef.orderByChild is not a function

答案1

得分: 0

这一部分代码混合了SDK版本/语法。

这一行使用了SDK版本9引入的模块化语法:

const query = query(usersRef, orderByChild('email'), equalTo(userEmail));

但在这一行中,您尝试使用一直存在的命名空间API调用:

emailQuery.on('value', (snapshot) => {

虽然两种语法都继续得到支持,但您不能随意混合它们。在单个文件中,您必须选择使用其中一种。

在模块化语法中,等效于 ref.on(...) 的是全局的 onValue 函数,如 Firebase 文档中关于监听值事件所示。

根据那个文档,您需要这样写:

onValue(emailQuery, (snapshot) => {
  ...
});
英文:

You're mixing SDK versions/syntaxes.

This line uses the modular syntax introduced in SDK version 9:

const query = query(usersRef, orderByChild('email'), equalTo(userEmail));

But in this line you then try to use the namespaced API calls that have always existed:

emailQuery.on('value', (snapshot) => {

While both syntaxes continue to be supported, you can't use mix and match them at will. In a single file you will have to use one or the other.

The equivalent of ref.on(...) in the modular syntax, is the global onValue function as shown in the Firebase documentation on listening for value events.

Based on that, you'd need:

onValue(emailQuery, (snapshot) => {
  ...
});

huangapple
  • 本文由 发表于 2023年7月12日 21:16:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76671014.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定