簡體   English   中英

Firebase功能端點冷啟動

[英]firebase functions endpoint cold starts

Firebase REST端點。 我得到很多NULL返回,尤其是在啟動時。 縱觀其他問題,我認為這是一個冷門。 我相信問題在於我正在使用在Firebase有機會返回數據集之前返回的回調。 我從@puf閱讀了有關Callabcks的評論-Frank-van-puffelen表示冷啟動。 因此,我正在嘗試重新寫一個諾言。 該代碼通常可以正常運行,但仍能獲得冷啟動NULL數據集。 我如何做到這一點?

 var functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(functions.config().firebase); //================================================================================================= // KeysFromAccountGet01 //================================================================================================= // This function is not working correctly because it often returns a NULL set. probably // because I am using callbacks instead of promises, and the callback returns before firebase // can return a query. Usually it works. // But I am fairly sure that I should be using PROMICES so as to wait for the data to arrive. // that said, I can not figure out how to do a promise. Everythign I have tried returns nothing. // some sugestions on how to do promises for this would be appreciated. //curl 'https://us-central1-test.cloudfunctions.net/KeysFromAccountGet01?account=dporras8' //curl 'https://us-central1-test.cloudfunctions.net/KeysFromAccountGet01?account=' //firebase deploy --only functions:KeysFromAccountGet01 exports.KeysFromAccountGet01 = functions.https.onRequest((req, res) =>{ var arr =[]; arr.push("1====+++starting"); arr.push("acount = "+ req.query.account); admin.database().ref('/newacctok/'+req.query.account+'/tok3/').on('value', function(snapshot){ snapshot.forEach(function(miniSnapShot){ var tt = miniSnapShot.val(); var json = ({ "key":miniSnapShot.key, "account":req.query.account, "uuid":tt.uuid, "ts2":tt.ts2, "token":tt.token }); arr.push(json); }) .then(res.status(200).send(arr)); }); //=================================== 

我不確定這些更改是否會對您的Null回報有所幫助。 請注意,我將on()使監聽器保持連接狀態on()更改為once() 另外,我已經看到Frank van Puffelen的警告,警告不要在HTTPS請求功能中執行異步處理。 我將嘗試找到他的答案/評論並添加它們。

exports.KeysFromAccountGet01 = functions.https.onRequest((req, res) => {
  var arr =[];
  arr.push("1====+++starting");
  arr.push("acount = "+ req.query.account);
  // note change from on() to once()
  admin.database().ref('/newacctok/'+req.query.account+'/tok3/').once('value')
    .then(snapshot => {
      snapshot.forEach(miniSnapShot => {
        var tt = miniSnapShot.val();
        var json = ({
          "key":miniSnapShot.key,
          "account":req.query.account,
          "uuid":tt.uuid,
          "ts2":tt.ts2,
          "token":tt.token
        });
        arr.push(json);
      });
      res.status(200).send(arr)
    });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM