firebase - 更改为 Firebase 3 后,我的 Firebase 队列没有执行任何操作

标签 firebase firebase-realtime-database firebase-queue

我几周前开始使用 Firebase,并在我的 Node.js 服务器代码上运行 Firebase 队列。客户端可以将消息推送到队列/任务,服务器将解析它们。在 Firebase 更改为版本 3 后,我无法复制此内容,即使 Firebase 队列已更新并且其他人似乎没有出现问题。这可能只是我的代码中的一个小错误,但几天后我还没有发现它,如果有人能为我指出它,我将不胜感激。

这里有我的服务器代码。每当将任务添加到队列中时,它都应该打印到控制台,但事实并非如此。最后一行的推送表明它能够连接到服务器。

var firebase = require('firebase');
var Queue = require('firebase-queue');

firebase.initializeApp({
  serviceAccount: 'serviceAccountCredentials.json',
  databaseURL: 'https://heretikchess-250ed.firebaseio.com/'
});

var ref = firebase.database().ref('queue');

var queue = new Queue(ref, function(data, progress, resolve, reject) {
  console.log('This should print whenever a task is pushed onto the queue.')
  console.log(data);
  setTimeout(function() {
  resolve();
  }, 1000);
});

ref.push('this is a push from the server');
//This push works fine, so there's no problem connecting to the server.

这是我的客户端代码。推送到队列/任务成功。

<!doctype html>
<html lang='en'>
<head>
    <meta charset="utf-8">
    <title>Why won't my code work?</title>
    <script src="https://www.gstatic.com/firebasejs/3.0.0/firebase.js"></script>
    <script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'></script>
</head>

<body>

    <div>
        <h3>Submit push to firebase</h3>
        <button id='submitbutton' class='btn btn-default' type='button'>Submit</button>
    </div>

<script>
      var config = {
        apiKey: "AIzaSyCJKI3tjnnuOIcx2rnOuSTUgncuDbbxfwg",
        authDomain: "heretikchess-250ed.firebaseapp.com",
        databaseURL: "https://heretikchess-250ed.firebaseio.com",
        storageBucket: "heretikchess-250ed.appspot.com",
      };
      firebase.initializeApp(config);

      var ref = firebase.database().ref('queue/tasks');

      //Send message to queue/tasks
    $('#submitbutton').click(function() {
        ref.push('this is a push from the client');
    })
    </script>
</body>
</html>

最佳答案

这对我有用:

server.js

var firebase = require('firebase');
var Queue = require('firebase-queue');

firebase.initializeApp({
  serviceAccount: 'serviceAccountCredentials.json',
  databaseURL: 'https://heretikchess-250ed.firebaseio.com/'
});

var db = firebase.database();
var ref = db.ref("queue");
var queue = new Queue(ref, function(data, progress, resolve, reject) {
  console.log('This should print whenever a task is pushed onto the queue.')
  console.log(data);
  setTimeout(function() { // NB: the record will be entirely removed when resolved
    resolve();
  }, 1000);
});

client.js

var firebase = require('firebase');
var Queue = require('firebase-queue');

firebase.initializeApp({
  serviceAccount: 'serviceAccountCredentials.json',
  databaseURL: 'https://heretikchess-250ed.firebaseio.com/'
});
var db = firebase.database();
var ref = db.ref("queue");

var task = {'userId': "Peter"};

// ref.child('tasks').push('this is a push from the client"}); 
// NB the above doesn't work because it's a string and not a structure
ref.child('tasks').push({"name": "this is a push from the client"}).then(function(){ process.exit();});

注意 - 您需要将结构而不是值发布到任务中。您可能会发现它可以工作如果而不是

ref.push('this is a push from the client');

你使用

ref.push({"name": "this is a push from the client"});

此外,在您尝试从网页执行作业请求的地方,我相信您还需要对用户进行身份验证,因为应用程序初始化我认为不会对用户进行身份验证,而只是识别连接。

index.html

<!doctype html>
<html lang='en'>
<head>
    <meta charset="utf-8">
    <title>Why won't my code work?</title>
    <script src="https://www.gstatic.com/firebasejs/3.0.0/firebase.js">       
</script>
<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'>    
</script>
</head>

<body>

    <div>
        <h3>Submit push to firebase</h3>
        <button id='submitbutton' class='btn btn-default' type='button'>Submit</button>
    </div>

<script>
  var config = {
    apiKey: "AIzaSyCJKI3tjnnuOIcx2rnOuSTUgncuDbbxfwg",
    authDomain: "heretikchess-250ed.firebaseapp.com",
    databaseURL: "https://heretikchess-250ed.firebaseio.com",
    storageBucket: "heretikchess-250ed.appspot.com",
  };
  firebase.initializeApp(config);

  var ref = firebase.database().ref('queue/tasks');

  // NEED TO AUTH THE CONNECTION AS A USER

  firebase.auth().signInWithEmailAndPassword('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="671702130215271714040813134904080a490612" rel="noreferrer noopener nofollow">[email protected]</a>', 'password').catch(function(error) {
    // Handle Errors here.
    var errorCode = error.code;
    var errorMessage = error.message;
    // ...
  }).then( function(){console.log('peter logged in')});


  //Send message to queue/tasks
$('#submitbutton').click(function() {
    ref.push({'frompeter':'this is a push from the client'});
})
</script>
</body>
</html>

请注意,您还需要添加用户并在/queue 路径的 auth 中配置访问权限。 我手动添加了[email protected]的用户名和密码通过 firebase auth/users 控制台屏幕。

您想要完善的入门配置。

{
    "rules": {
        ".read": false,
        ".write": false,
        "queue": {
        ".read": true,
        ".write": true
        }
    }
}

如果您按照上面的方式打开了身份验证,那么您将能够使用curl从命令行测试客户端请求:

curl -X POST -d '{"foo": "bar"}' https://heretikchess-250ed.firebaseio.com//queue/tasks.json 

如果仍然有问题,也许可以从 shell 检查所有库版本。

## Display node version
npm -v
##  ( mine is 3.9.0 )

npm list firebase
## mine is 3.0.2

npm list firebase-queue
## mine is 1.4.0

关于firebase - 更改为 Firebase 3 后,我的 Firebase 队列没有执行任何操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37406271/

相关文章:

swift - Firebase Swift 查询和客户端扇出

firebase - 如何执行Firebase功能的域验证

ios - 无法使用 Firebase 检索最近添加的值

ios - FirebaseDatabase Web iOS/OSX

java - 如何使用多个 firebase 项目填充 ListView

ios - Firebase:如何为不同的用户提供读/写访问权限?

Firebase Cloud Firestore "Request/response"文档

node.js - 仅将 worker dyno 部署到 heroku(用于 firebase-queue)

java - 我的适配器不会更新值,直到我返回然后再次返回该 Activity 。什么原因

node.js - Node GeoFire 内存泄漏?