我在 Google App Engine 柔性环境中运行 firebase。我在一个节点上设置了一个子添加侦听器,每次添加一个子时它都会被调用两次。这只发生在我在 Google App Engine 上运行此代码时。如果我在本地运行它,它会按预期运行。这是代码
// [START app]
'use strict';
var express = require('express');
var bodyParser = require('body-parser');
var request = require('request');
var firebase = require('firebase-admin');
var app = express();
firebase.initializeApp({
credential: firebase.credential.applicationDefault(),
databaseURL: "https://myurl.firebaseio.com/"
});
var db = firebase.database();
var globalNotifications = db.ref("/globalNotifications");
var API_KEY = "myKey"; // Your Firebase Cloud Server API key
var now = new Date();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
globalNotifications.orderByChild('processed').equalTo(0).on("child_added", function(snapshot) {
var key = snapshot.key;
var incomingNotification = snapshot.val();
var userID = incomingNotification.userID;
var notifyID = incomingNotification.notifyID;
request({
url: 'https://fcm.googleapis.com/fcm/send',
method: 'POST',
headers: {
'Content-Type' :'application/json',
'Authorization': 'key='+API_KEY
},
body: JSON.stringify({
"to" : incomingNotification.notificationID,
"priority" : "high",
"notification" : {
"body" : "someone is following you",
"title" : "You have a new follower"
}
})
}, function(error, response, body) {
if (error) { console.error(error); }
else if (response.statusCode >= 400) {
console.error('HTTP Error: '+response.statusCode+' - '+response.statusMessage);
}
else {
console.log(response.statusCode);
console.log(body);
globalNotifications.child(key).update({"processed": 1, "response": body});
}
});
});
// Start the server
var server = app.listen(process.env.PORT || '8080', function () {
console.log('App listening on port %s', server.address().port);
console.log('Press Ctrl+C to quit.');
});
// [END app]
感谢您提前提供任何帮助。