【发布时间】:2017-08-10 22:28:21
【问题描述】:
在我的 Ionic 应用中,当用户访问“位置”选项卡时,会对用户的帐户和相关业务进行一些操作。
现在,当一个键进入(基于 Geofire)时,一切都会被触发,但是它会触发多次,我不知道为什么!
例如,以毫秒为单位的时间将在推送时触发两次,并且推送的消息一次推送 8 或 9 次。
代码如下:
// Listen to every people in our query...
geoQuery40.on("key_entered", function(key) {
// ... and look them up by key ...
restaurantsRef40.child(key).on("value", function(snapshot42) {
var restaurant40 = snapshot42.val();
var displayBusinessName = snapshot42.val().name;
var displayBusinessDescription = snapshot42.val().description;
var displayBusinessUid = snapshot42.val().uid;
firebase.database().ref('accounts/' + userId + '/currentBusiness/').update({
name: displayBusinessName,
description: displayBusinessDescription,
})
//add the user in the Business' rotary
var refRotary = firebase.database().ref('business/' + displayBusinessUid + '/rotary/' + userId);
refRotary.once('value', function(snapshot78) {
var displayVisit = snapshot78.val().lastVisit;
var displayCurrentVisit = snapshot78.val().currentVisit;
var displayWelcomeMessage = snapshot78.val().welcomeMessage;
var displayBusinessPic = snapshot78.val().photoURL;
$timeout(function() {
$scope.displayVisit = displayVisit;
$scope.displayCurrentVisit = displayCurrentVisit;
$scope.displayWelcomeMessage = displayWelcomeMessage;
$scope.displayBusinessPic = displayBusinessPic;
if (displayCurrentVisit === undefined){
firebase.database().ref('business/' + displayBusinessUid + '/rotary/' + userId).update({
uid: userId,
lastVisit: Date.now(),
currentVisit: Date.now(),
})
} else {
firebase.database().ref('business/' + displayBusinessUid + '/rotary/' + userId).update({
uid: userId,
lastVisit: displayCurrentVisit,
currentVisit: Date.now(),
})
}
if (Date.now() - displayVisit > 3600000){
firebase.database().ref('business/' + displayBusinessUid + '/rotary/' + userId + '/visits/').push({
visit: Date.now(),
})
}
var d = new Date();
var g = new Date();
d = d.toLocaleTimeString().replace(/:\d+ /, ' ');
g = g.toLocaleTimeString().replace(/:\d+ /, ' ') + new Date();
var refRotary2 = firebase.database().ref('business/' + displayBusinessUid);
refRotary2.once('value', function(snapshot79) {
var displayWelcomeMessage = snapshot79.val().welcomeMessage;
var displayBusinessPic = snapshot79.val().photoURL;
$timeout(function() {
$scope.displayWelcomeMessage = displayWelcomeMessage;
$scope.displayBusinessPic = displayBusinessPic;
// envoie un message directement si c'est la premiere fois de la journee.
var database = firebase.database();
firebase.database().ref().child('/accounts/' + userId + '/discussions/' + userId + displayBusinessUid).set({
blocked: 0,
name: displayBusinessName,
profilePic: displayBusinessPic,
lastMessage: displayWelcomeMessage,
time: d,
discussionId: userId + displayBusinessUid,
newMessages: 1,
userId: displayBusinessUid,
})
firebase.database().ref().child('/messages/' + userId + displayBusinessUid).push({
name: displayBusinessName,
profilePic: displayBusinessPic,
text: displayWelcomeMessage,
time: d,
userId: displayBusinessUid,
})
firebase.database().ref().child('/accounts/' + displayBusinessUid + '/discussions/' + userId + displayBusinessUid).set({
name: displayBusinessName,
profilePic: displayBusinessPic,
lastMessage: displayWelcomeMessage,
time: d,
newMessages: 1,
userId: displayBusinessUid,
})
// fin du message
})
})
})
})
//end business rotary
});
});
【问题讨论】:
标签: javascript angularjs firebase ionic-framework geofire