Javascript Firebase HTTPS云功能触发两次
我已经阅读了Firebase云函数,并试图确定为什么我的函数会被触发两次,但尚未找到成功的解决方案。我也尝试过作为一种解决方法,但是它的最新更新表明云功能是一种可行的方法 简而言之,我正在使用Javascript Firebase HTTPS云功能触发两次,javascript,json,firebase,firebase-realtime-database,request-promise,Javascript,Json,Firebase,Firebase Realtime Database,Request Promise,我已经阅读了Firebase云函数,并试图确定为什么我的函数会被触发两次,但尚未找到成功的解决方案。我也尝试过作为一种解决方法,但是它的最新更新表明云功能是一种可行的方法 简而言之,我正在使用请求承诺从外部API检索通知,将这些通知与我数据库中已有的通知进行核对,并在识别出新通知后将其发布到所述数据库。然后根据新通知更新相应的地点。代码如下: 'use strict'; const functions = require('firebase-functions'); const admin =
请求承诺
从外部API检索通知,将这些通知与我数据库中已有的通知进行核对,并在识别出新通知后将其发布到所述数据库。然后根据新通知更新相应的地点。代码如下:
'use strict';
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const request = require('request');
const rp = require('request-promise');
admin.initializeApp(functions.config().firebase);
const db = admin.database();
const venues = db.ref("/venues/");
exports.getNotices = functions.https.onRequest((req, res) => {
var options = {
uri: 'https://xxxxx.xxxxx',
qs: {
format: 'json',
type: 'venue',
...
},
json: true
};
rp(options).then(data => {
processNotices(data);
console.log(`venues received: ${data.length}`);
res.status(200).send('OK');
})
.catch(error => {
console.log(`Caught Error: ${error}`);
res.status(`${error.statusCode}`).send(`Error: ${error.statusCode}`);
});
});
function processNotices(data) {
venues.once("value").then(snapshot => {
snapshot.forEach(childSnapshot => {
var existingKey = childSnapshot.val().key;
for (var i = 0; i < data.length; i++) {
var notice = data[i];
var noticeKey = notice.key;
if (noticeKey !== existingKey) {
console.log(`New notice identified: ${noticeKey}`)
postNotice(notice);
}
}
return true;
});
});
}
function postNotice(notice) {
var ref = venues.push();
var key = ref.key;
var loc = notice.location;
return ref.set(notice).then(() => {
console.log('notice posted...');
updateVenue(key, loc);
});
}
function updateVenue(key, location) {
var updates = {};
updates[key] = "true";
var venueNoticesRef = db.ref("/venues/" + location + "/notices/");
return venueNoticesRef.update(updates).then(() => {
console.log(`${location} successfully updated with ${key}`);
});
}
“严格使用”;
const functions=require('firebase-functions');
const admin=require('firebase-admin');
const request=require('request');
const rp=要求(“请求-承诺”);
admin.initializeApp(functions.config().firebase);
const db=admin.database();
const VICENTES=db.ref(“/VICENTES/”);
exports.getNotices=functions.https.onRequest((req,res)=>{
变量选项={
uri:'https://xxxxx.xxxxx',
qs:{
格式:“json”,
类型:'地点',
...
},
json:true
};
rp(选项)。然后(数据=>{
处理通知(数据);
log(`VICENCES received:${data.length}`);
res.status(200)。发送('OK');
})
.catch(错误=>{
log(`captured Error:${Error}`);
res.status(`error.statusCode}`)。send(`error:${error.statusCode}`);
});
});
函数processNotices(数据){
地点。一次(“值”)。然后(快照=>{
snapshot.forEach(childSnapshot=>{
var existingKey=childSnapshot.val().key;
对于(变量i=0;i{
console.log('noticeposted…');
更新(键,loc);
});
}
函数更新(键、位置){
var更新={};
更新[键]=“真”;
var venueNoticesRef=db.ref(“/victions/”+location+“/notices/”);
返回VenuNoticesRef.update(更新)。然后(()=>{
log(`${location}已成功更新为${key}`);
});
}
如有任何关于如何纠正双重触发的建议,将不胜感激。提前谢谢 问题已解决-Firebase控制台日志中的一些错误信息(重复条目),加上嵌套的
for
循环顺序错误,导致了明显的双重触发 问题已解决-Firebase控制台日志中的一些错误信息(重复条目),加上嵌套的for
循环顺序错误,导致了明显的双重触发 如何调用它,使其只执行一次?日志显示了什么?还请注意,您正在执行异步数据库工作,而无需等待它在processNotices()中完成。它应该返回一个承诺,以便调用方在向客户端发送响应之前能够知道数据库工作何时完全完成。谢谢您的提示,@DougStevenson。我找到了一个解决方案(详细如下)。您如何调用它,使它只执行一次?日志显示了什么?还请注意,您正在执行异步数据库工作,而无需等待它在processNotices()中完成。它应该返回一个承诺,以便调用方在向客户端发送响应之前能够知道数据库工作何时完全完成。谢谢您的提示,@DougStevenson。我找到了一个解决方案(详情如下)。