幾周前我開始使用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>
我認爲你需要使用隊列/任務子樹,雖然我剛剛開始。你也可以通過以下方式確認你的環境: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 –