使用firebase实时数据库添加react中的朋友,添加多个朋友,而不是一个

发布于 2025-02-10 12:54:58 字数 1277 浏览 1 评论 0原文

我目前正在使用React和Firebase的实时数据库进行一个项目,并尝试实现朋友网络。目前,我的问题列于以下:用户可以将电子邮件输入输入。一旦提交了此电子邮件,将对整个用户数据库进行快照,并且将使用一个foreach循环循环循环,直到找到包含用户输入的电子邮件的数据(每个用户都有电子邮件,用户名,,密码和UID字段)。一旦找到它,包含输入电子邮件的用户数据将被复制到当前用户朋友列表中。

尽管我的代码似乎可以工作,但仍有很多奇怪的问题。在一个测试中,我创建了三个帐户,并有一个帐户添加另一个帐户。如果我创建另一个帐户,有时会自动将上一个用户的朋友添加到当前用户。我还发现了一种情况,在制作一个新帐户并添加一个已经有朋友的新帐户时,这些用户的朋友将被添加到当前的朋友中。我无可救药地失去了为什么会发生这种情况。

import React, {useState, useEffect} from 'react'
import {db, auth} from '../config/fire'
import Message from './Message';

function AddFriend() {
  
  const [friend, setFriend] = useState("");
  const [text, setText] = useState("");
  let ref = db.ref(`Users`);

  const handleText = (e) => {
      setText(e.target.value);
  }

  function handleSubmit(e)  {
      ref.on('value', (snapshot) => {
        let newFriend = "";
        snapshot.forEach(data => {
          if (data.val().email===text && data.val().email!==auth.currentUser.email)  {
            const newRef = db.ref(`Users/${auth.currentUser.uid}/friends/${data.val().uId}}`);
            newRef.update({email: data.val().email, name: data.val().name, password: data.val().password, uId: data.val().uId});
            setText("")
          }
          
        })
      },[]);
      setText("");
  }

I am currently working on a project using React and Firebase's realtime database and trying to implement a friend network. Currently my issues resides in the following: the user can enter an email into an input. Once they submit this email, a snapshot will be taken of the entire User database and a foreach loop will be used to loop over it until it finds the piece of data that contains the email the user entered (Each user has an email, username, password, and uId field). Once its found, that users data who contains the email that was entered will be copied over into the current users friends list.

While my code should seem to work, there are a lot of weird issues. In one test, I make three accounts, and have one account add the other two. If I make another account, it will sometimes automatically add the previous user's friends to the current user. I've also found one case where when making a new account and adding a new one that already has friends, those user's friends will be added to the current one. I am hopelessly lost as to why this may be happening.

import React, {useState, useEffect} from 'react'
import {db, auth} from '../config/fire'
import Message from './Message';

function AddFriend() {
  
  const [friend, setFriend] = useState("");
  const [text, setText] = useState("");
  let ref = db.ref(`Users`);

  const handleText = (e) => {
      setText(e.target.value);
  }

  function handleSubmit(e)  {
      ref.on('value', (snapshot) => {
        let newFriend = "";
        snapshot.forEach(data => {
          if (data.val().email===text && data.val().email!==auth.currentUser.email)  {
            const newRef = db.ref(`Users/${auth.currentUser.uid}/friends/${data.val().uId}}`);
            newRef.update({email: data.val().email, name: data.val().name, password: data.val().password, uId: data.val().uId});
            setText("")
          }
          
        })
      },[]);
      setText("");
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帝王念 2025-02-17 12:54:58

弄清楚了。您需要使用ref.once而不是ref.on。

Figured it out. You need to use ref.once instead of ref.on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文