【发布时间】: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