每次用户在我的聊天应用程序中发送消息时,我都会尝试复制此数据结构:
{
entries: {
users: {
"twitter username": {
"unique key assigned by firebase": {
name: "twitter username",
text: "whatever which user writes"
}
}
}
}
}
所以我尝试了这个:
var entryRef = new Firebase('https://ranchat.firebaseio.com:443/entries/users/');
var messagesRef = entryRef.child(name);
// When the user presses enter on the message input, write the message to firebase.
$('#messageInput').keypress(function (e) {
if (e.keyCode == 13) {
// You shouldn't allow an empty entry
var text = $('#messageInput').val();
messagesRef.push({name:name, text:text});
$('#messageInput').val('');
}
});
( Here is the whole code of my app )
但是消息只会发送到 firebase 一次。那就是我可以在 <input>
上写一条消息,按 Enter 并将其发送到 Firebase ( the data appear in it ),但如果我想重复相同的操作,数据将不会出现在其中。由于用户必须使用 Twitter 登录才能发送消息,因此我尝试从其他 session (使用其他 Twitter 帐户登录)发送数据,但没有成功;在第一条消息之后,无论是同一用户还是其他用户,都无法发送数据。
当我尝试向 Firebase 发送另一条消息时,我在 JS 控制台中收到此错误:
"FIREBASE WARNING: set at /entries/users/jobsamuel/-JHXOGvd2Gw1Z8UAZ5X8 failed: permission_denied "
我不知道我做错了什么或者我没有做什么。
最佳答案
我在我的 Firebase Forge 的安全规则中修改了 data.exists()
验证并且它有效:
{
"rules": {
".read": true,
"$comment": {
".write": "!data.exists() && newData.child('twitter_id').val() == auth.id"
}
}
}
问题是 !。当我复制 Firebase 简单登录 (Twitter) 的身份验证有效负载时,我没有意识到可能的影响。现在,根据此规则,数据将按照我的意愿发送:
{
"rules": {
".read": true,
".write": "data.exists() && newData.child('twitter_id').val() == auth.id"
}
}
—
但并非一切都是完美的。我不明白两件事。
第一个:使用新规则,如果 root directory is empty 我无法将数据发送到 Firebase .我只需要 .push()
或 .set()
之前的东西。
第二个:我需要在我的 Firebase 引用中将 : 放在 .com 之后;如果我不输入 : 并且我不知道为什么,则不会发送数据。
var entryRef = new Firebase('https://ranchat.firebaseio.com:/entries/');
var messagesRef = entryRef.child(name).child('messages');
// When the user presses enter on the message input, write the message to firebase.
$('#messageInput').keypress(function (e) {
if (e.keyCode == 13) {
// You shouldn't allow an empty entry
var text = $('#messageInput').val();
messagesRef.push({name:name, text:text});
$('#messageInput').val('');
}
});
嗯……目前,我觉得我很好。感谢您的帮助!
关于javascript - 我的应用无法多次向 Firebase 发送相同的数据结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22233020/