如何使用Firebase中的用户帐户将数据发送到数据库?

时间:2019-11-14 18:42:37

标签: javascript reactjs firebase firebase-realtime-database firebase-authentication

我对Firebase有问题。 我知道如何使用Firebase框架创建电子邮件帐户,但是我不知道如何使用电子邮件帐户将数据发送到数据库。

示例:

您发表评论,其他用户希望看到它。

我已经看过Firebase文档,但是他们没有说如何使用帐户发送数据。 您有解决我问题的方法吗?预先感谢。

1 个答案:

答案 0 :(得分:0)

我不确定我是否正确理解你。但是,如果您尝试将消息发送给其他用户,则通常会得到一个模型化聊天室的数据库结构。对于1:1聊天室,我建议使用我在此处显示的结构:Best way to manage Chat channels in Firebase

然后,您可以在代码中向该房间添加类似以下内容的消息:

let myUid = firebase.auth().currentUser.uid;
let otherUid = "uidOfTheOtherUser"; // This is a value you must 'know" somehow
let chatRoomId = 'chat_'+(myUid<otherUid ? myUid+'_'+otherUid : otherUid+'_'+myUid);

let roomRef = firebase.database().ref(chatRoomId);
roomRef.push({
  text: "This is my message",
  uid: myUid,
  timestamp: firebase.database.ServerValue.TIMESTAMP
});