为什么我的 firebase 回调会被多次触发?
Why are my firebase callbacks being triggered multiple times?
我有一个小型节点服务器监听 firebase 的变化并在特定条件下发送电子邮件。这是代码:
var Firebase = require('firebase');
var ref = new Firebase(process.env.FIREBASE_URL);
ref.authWithCustomToken(process.env.FIREBASE_SECRET, function (err) {
if (err) {
console.log(new Date().toString(), 'Firebase Authentication Failed!', err);
EmailService.send('Firebase authentication failed', 'errors@domain.com', err);
} else {
ref.child('applicants').on('child_added', function (snapshot) {
var applicant = snapshot.val();
if (!(applicant.alerts && applicant.alerts.apply)) {
console.log(new Date().toString(), 'New Applicant: ', applicant);
var body = applicant.firstName + ' ' + applicant.lastName + '\n' + applicant.email + '\n' + applicant.phoneNumber;
EmailService
.send('New Applicant', 'applicants@entercastle.com', body)
.then(function () {
ref.child('applicants').child(snapshot.key()).child('alerts').child('apply').set(true);
})
.catch(function (err) { console.log(new Date().toString(), err); });
}
});
}
});
但是,我不断收到重复的电子邮件。最奇怪的是,尽管发送了多封电子邮件,但日志只显示每个申请人的单个 "New Applicant : ..." 声明。
知道是什么原因造成的或如何解决吗?
谢谢!
您的 child_added 事件将在每次 authWithCustomToken() 成功时触发。每次重新加载或重新验证页面时,都会附加新的侦听器,每个用户都会触发一个新的 child_added 事件,并且会重新发送电子邮件。
The child_added event is typically used when retrieving a list of
items in Firebase. Unlike value which returns the entire contents of
the location, child_added is triggered once for each existing child
and then again every time a new child is added to the specified path.
The event callback is passed a snapshot containing the new child's
data.
(强调我的)
如果您只想发送一次电子邮件,更好的方法是使用 queue strategy,其中您 "queue" 一个 activity(例如欢迎电子邮件)用户被创建。
然后您的服务可以读取队列并在任务成功完成后删除任务。这样,你就不会再重复了。
在添加新侦听器之前删除现有侦听器将解决此问题
在on()
事件
之前尝试这个off()
事件
ref.child('applicants').off(); // it will remove existing listener
然后是你的代码
ref.child('applicants').on('child_added', function(snapshot) {
var applicant = snapshot.val();
if (!(applicant.alerts && applicant.alerts.apply)) {
console.log(new Date().toString(), 'New Applicant: ', applicant);
var body = applicant.firstName + ' ' + applicant.lastName + '\n' + applicant.email + '\n' + applicant.phoneNumber;
EmailService
.send('New Applicant', 'applicants@entercastle.com', body)
.then(function() {
ref.child('applicants').child(snapshot.key()).child('alerts').child('apply').set(true);
})
.catch(function(err) {
console.log(new Date().toString(), err);
});
}
});
我有一个小型节点服务器监听 firebase 的变化并在特定条件下发送电子邮件。这是代码:
var Firebase = require('firebase');
var ref = new Firebase(process.env.FIREBASE_URL);
ref.authWithCustomToken(process.env.FIREBASE_SECRET, function (err) {
if (err) {
console.log(new Date().toString(), 'Firebase Authentication Failed!', err);
EmailService.send('Firebase authentication failed', 'errors@domain.com', err);
} else {
ref.child('applicants').on('child_added', function (snapshot) {
var applicant = snapshot.val();
if (!(applicant.alerts && applicant.alerts.apply)) {
console.log(new Date().toString(), 'New Applicant: ', applicant);
var body = applicant.firstName + ' ' + applicant.lastName + '\n' + applicant.email + '\n' + applicant.phoneNumber;
EmailService
.send('New Applicant', 'applicants@entercastle.com', body)
.then(function () {
ref.child('applicants').child(snapshot.key()).child('alerts').child('apply').set(true);
})
.catch(function (err) { console.log(new Date().toString(), err); });
}
});
}
});
但是,我不断收到重复的电子邮件。最奇怪的是,尽管发送了多封电子邮件,但日志只显示每个申请人的单个 "New Applicant : ..." 声明。
知道是什么原因造成的或如何解决吗?
谢谢!
您的 child_added 事件将在每次 authWithCustomToken() 成功时触发。每次重新加载或重新验证页面时,都会附加新的侦听器,每个用户都会触发一个新的 child_added 事件,并且会重新发送电子邮件。
The child_added event is typically used when retrieving a list of items in Firebase. Unlike value which returns the entire contents of the location, child_added is triggered once for each existing child and then again every time a new child is added to the specified path. The event callback is passed a snapshot containing the new child's data.
(强调我的)
如果您只想发送一次电子邮件,更好的方法是使用 queue strategy,其中您 "queue" 一个 activity(例如欢迎电子邮件)用户被创建。
然后您的服务可以读取队列并在任务成功完成后删除任务。这样,你就不会再重复了。
在添加新侦听器之前删除现有侦听器将解决此问题
在on()
事件
off()
事件
ref.child('applicants').off(); // it will remove existing listener
然后是你的代码
ref.child('applicants').on('child_added', function(snapshot) {
var applicant = snapshot.val();
if (!(applicant.alerts && applicant.alerts.apply)) {
console.log(new Date().toString(), 'New Applicant: ', applicant);
var body = applicant.firstName + ' ' + applicant.lastName + '\n' + applicant.email + '\n' + applicant.phoneNumber;
EmailService
.send('New Applicant', 'applicants@entercastle.com', body)
.then(function() {
ref.child('applicants').child(snapshot.key()).child('alerts').child('apply').set(true);
})
.catch(function(err) {
console.log(new Date().toString(), err);
});
}
});