2017-06-09 16 views
0

Firebase RESTエンドポイント。特に起動時に多くのNULLが返されています。他の問題を見てみると、それはコールドスタートだと思います。私は、firebaseがデータセットを返すチャンスを得る前に、私がコールバックを使用して戻っていることが問題だと考えています。私は@pufからのcallabcksについてのコメントを読んだ - frank-van-puffelen コールドスタートをsugesting。だから私は約束として書き直そうとしている。このコードは通常動作しますが、まだコールドスタートNULLデータセットを取得します。私はこれをどのように約束するのでしょうか?firebase functionsエンドポイントコールドスタート

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

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

+0

約束どおりに動作しても差はありません。ネストされた複数のロードがあるときにコードを読みやすくするだけです。 'send(...)'をコールバックに移したとき、どんな問題がありますか? –

+0

そして、Bobが言うことを確実にしてください: 'once()'を使ってください。 HTTP関数では、継続的なリスナーの使用はありません。 –

+0

あなたはそれを解決しましたか? –

答えて

1

私は、これらの変更は、あなたのNULLを返して役立つか分かりません。リスナーを接続したままの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) 
    }); 
}); 
関連する問題