javascript - 我的应用无法多次向 Firebase 发送相同的数据结构

标签 javascript firebase

每次用户在我的聊天应用程序中发送消息时,我都会尝试复制此数据结构:

{
   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/

相关文章:

android - Xamarin.android 默认 FirebaseApp 未在此过程中初始化

javascript - 如何在 NextJS 中间件中获取查询字符串参数

javascript - 如何修复react中eslint的 "Component is defined but never used"?

javascript - 如何在 Firestore NextJS 上执行内连接查询

error-handling - 新的Firebase初始化失败

ios - Swift FIRMessaging 在注册时发送推送通知请求

android - 无法解析com.firebase.firebaseui:firebase-ui-auth:4.3.1

javascript - 在 JavaScript 中获取跨度的 id。

javascript - 客户端和服务器端编程有什么区别?

javascript - 如何使页脚和页眉固定,而表格中间可滚动