2017-06-26 279 views
0

我已阅读Firebase云端函数reference,guidessample code,试图确定为什么我的功能被触发两次,但尚未找到成功的解决方案。我还试用了Firebase-Queue作为解决方法,但是其最新更新表明云功能是一种方法。Firebase HTTPS云端功能触发两次

总之,我使用request-promise检索来自外部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}`); 
    }); 
} 

有关如何纠正双触发的任何建议将不胜感激。提前致谢!

+0

你是如何调用它,使它只应执行一次?日志显示了什么? –

+0

另请注意,您正在执行异步数据库工作而无需等待它在processNotices()中完成。它应该返回一个承诺,以便在向客户端发送响应之前,调用者可以知道数据库的工作何时完全完成。 –

+0

感谢您的提示,@DougStevenson。我找到了一个解决方案(详见下文)。 – donovki

回答

0

问题已解决 - Firebase控制台中的一些错误信息日志(重复条目),以及顺序错误的嵌套for循环负责明显的双重触发。