2016-05-24 33 views
2

幾周前我開始使用Firebase,並在我的node.js服務器代碼上運行了Firebase隊列。客戶可以將消息推送到隊列/任務,服務器將解決它們。 Firebase已更改爲第3版後,我無法複製此內容,即使Firebase隊列已更新且沒有其他人似乎遇到問題。這可能只是我的代碼中的一個小錯誤,但幾天後我還沒有找到它,如果有人能爲我指出,我將不勝感激。我更改爲Firebase後我的Firebase隊列無法執行任何操作3

這裏我有我的服務器代碼。每當任務被添加到隊列中時,它應該打印到控制檯,但事實並非如此。最後一行顯示它能夠連接到服務器。

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> 
+0

我認爲你需要使用隊列/任務子樹,雖然我剛剛開始。你也可以通過以下方式確認你的環境:npm list firebase-queue,npm -v和npm list firebase,以確保我使用的是相同的版本。也可能想檢查出http://stackoverflow.com/questions/36460698/how-push-a-task-with-specid-in-firebase-queue-with-nodejs?rq=1 –

回答

2

這個工作對我來說:

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('[email protected]', '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> 

請注意,您還需要添加用戶並配置訪問權限的身份驗證爲/隊列路徑。 我通過firebase身份驗證/用戶控制檯屏幕手動添加了[email protected]的用戶名和密碼。

您需要優化的初學者配置。

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

如果仍然有問題,也許是檢查所有的庫版本貝殼。

## 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 
相關問題