【问题标题】:How to send data to database with user account from Firebase?如何使用 Firebase 的用户帐户将数据发送到数据库?
【发布时间】:2019-11-14 18:42:37
【问题描述】:
我在使用 Firebase 时遇到了问题。
我知道如何使用 Firebase 框架创建电子邮件帐户,但我不知道如何使用电子邮件帐户将数据发送到我的数据库。
例子:
您发表评论,而其他用户希望看到该评论。
我已经看过 Firebase 文档,但他们没有说明如何使用帐户发送数据。
你有我的问题的解决方案吗?提前致谢。
【问题讨论】:
标签:
javascript
reactjs
firebase
firebase-realtime-database
firebase-authentication
【解决方案1】:
我不确定我是否理解正确。但是,如果您尝试向其他用户发送消息,您通常会得到一个模拟聊天室的数据库结构。对于 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
});