几周前我开始使用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 –