在更改为Firebase 3后,我的Firebase队列不会执行任何操作 [英] My Firebase Queue doesn't do anything after I changed to Firebase 3

查看:134
本文介绍了在更改为Firebase 3后,我的Firebase队列不会执行任何操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几周前我开始使用Firebase,并在我的node.js服务器代码上运行了Firebase Queue。客户可以将消息推送到队列/任务,服务器将解决这些问题。 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('这个应该在任务被压入队列时打印出来')
console.log(data);
setTimeout(function(){
resolve();
},1000);
});

ref.push('这是从服务器推');
//这个推送工作正常,所以没有问题连接到服务器。

这里是我的客户端代码。

 <!doctype html> 
< html lang ='en'>
< head>
< meta charset =utf-8>
< title>为什么我的代码不能工作?< / 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>将推送到firebase< / h3>
< button id ='submitbutton'class ='btn btn-default'type ='button'>提交< / 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');
$ b $ //发送消息到队列/任务
$('#submitbutton')。click(function(){
ref.push('这是从客户端');
})
< / script>
< / body>
< / html>


解决方案


$ b

server.js
$ b

  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('这应该在任务被压入队列时打印')
();
},$ b $ setTimeout });

client.js


$ b $ pregen =lang-js prettyprint-override> 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};
$ b $ // ref.child('tasks')。push('this is a push from the client});
//注意上面的不行,因为它是一个字符串而不是结构
ref.child('tasks')。push({name:this is a push from the client})。then(function(){process.exit();}) ;

注意 - 你需要发布一个结构而不是任务的值,你可能会发现它如果不是

  ref.push('这是来自客户端的推送'); 

您使用

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

你正试图执行从网页的工作要求我相信你也将需要认证的用户作为应用程序初始化不我认为认证用户,但只是标识连接。

index.html
$ b

<!d octype html>
< html lang ='en'>
< head>
< meta charset =utf-8>
< title>为什么我的代码不能工作?< / 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>将推送到firebase< / h3>
< button id ='submitbutton'class ='btn btn-default'type ='button'>提交< / 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');

//需要连接为用户

firebase.auth()。signInWithEmailAndPassword('peter@pscott.com.au','password')。catch (函数(错误){
//处理错误在这里
var errorCode = error.code;
var errorMessage = error.message;
// ... $ b $ ()(){console.log('peter logged in')});


发送消息到队列/任务
$('#submitbutton')。click(function(){
ref.push({'frompeter' :'这是从客户端推送'});
})
< / script>
< / body>
< / html>

请注意,您还需要添加用户并在身份验证中为/队列路径。
我通过firebase身份验证/用户控制台屏幕手动添加了peter@pscott.com.au和密码的用户名。



您想要的初学者配置以改进。


$ b

{
rules:{
。读取:false,
.write:false,
queue:{
.read:true,
.write:true
}




如果你有auth打开如上那么你将能够使用curl来测试来自命令行的客户端请求:

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



  ##显示节点版本
npm -v
##(我的是3.9.0)

npm list firebase
## mine是3.0.2

npm list firebase-q ueue
## mine是1.4.0


I started using Firebase a couple of weeks ago, and had Firebase Queue running on my node.js server code. Clients could push messages to queue/tasks, and the server would resolve them. I cannot replicate this after Firebase has changed to version 3, even though Firebase Queue has been updated and no-one else seems to be having problems. It is probably just a minor bug in my code, but I haven't found it after a couple of days, and I would appreciate it if someone could point it out for me.

Here I have my server code. It should print to the console whenever a task is added to the queue, but it isn't. The push on the last line demonstrates that it is able to connect to the server.

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.

And here is my client code. The pushes to queue/tasks are successful.

<!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>

解决方案

This works for me:

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();});

NB - you need to post a structure and not a value into tasks. You may find that it works if instead of

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

you use

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

Also where you are trying to perform the job requests from the web page I believe that you will also need to authenticate the user as the app initialize does not I believe authenticate the user but merely identifies the connection.

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('peter@pscott.com.au', '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>

Note that you will also need to add the user and configure the access permissions in the auth for the /queue path. I manually added the username for peter@pscott.com.au and password through the firebase auth/users console screen.

A starter config that you would want to refine.

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

If you have the auth open as above then you will be able to test a client request from the command line using curl:

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

If still having problems perhaps check all your library versions from 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 3后,我的Firebase队列不会执行任何操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆