【问题标题】:firebase functions endpoint cold startsfirebase 函数端点冷启动
【发布时间】:2017-06-09 11:57:31
【问题描述】:

Firebase REST 端点。我收到很多 NULL 返回,尤其是在启动时。纵观其他问题,我认为这是一个冷启动。我认为问题在于我正在使用在firebase有机会返回数据集之前返回的回调。我从@puf - frank-van-puffelen 读到了关于 callabcks 的评论 建议冷启动。所以我试图重写作为一个承诺。此代码通常有效,但仍会获得冷启动 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));
});

//===================================

【问题讨论】:

  • 承诺不会对行为产生任何影响。当您有多个嵌套加载时,它们只会使代码更易于阅读。将send(...)移入回调有什么问题?
  • 并且绝对按照 Bob 所说的去做:使用 once()。在 HTTP 函数中连续监听器没有用处。
  • 你解决了吗?

标签: javascript firebase google-cloud-functions


【解决方案1】:

我不确定这些更改是否有助于您返回 Null。请注意,我将on() 更改为once(),这使监听器保持连接状态。另外,我看到 Frank van Puffelen 的回答警告不要在 HTTPS 请求函数中执行异步处理。我会尝试找到他的答案/cmets并添加它们。

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)
    });
});

【讨论】:

    猜你喜欢
    • 2022-01-08
    • 2022-01-20
    • 2022-01-22
    • 2020-12-23
    • 1970-01-01
    • 2022-11-10
    • 2021-07-16
    • 2020-05-18
    • 2021-06-23
    相关资源
    最近更新 更多