【问题标题】:Cloud Functions for Firebase Time Out w/ wrong response用于 Firebase 超时且响应错误的云函数
【发布时间】:2018-04-14 15:05:02
【问题描述】:

新手问题:Cloud Function 每次运行都会超时。

此外,它仅返回 ONE 值,即函数日志中的第一个 userId,并且不返回任何子项。我假设这是因为它调用 .once 然而,它在 forEach 循环中,所以我不确定它想要什么。

Firebase 数据库

-items
---- userId0123456789
   ---- randomKey987654321
       -- itemName
       -- itemDate
       -- itemType
---- userId987654321
   ---- randomKey012345678
       -- itemName
       -- itemDate
       -- itemType

这里是功能代码...

  const key = req.query.key;

  **let userID = 'xxxxx';
  let ikey = 'xxx';**

  var dbRef = admin.database().ref('/items/{userID}/{ikey}');

                dbRef.once("value", function(snapshot) {
                  snapshot.forEach(function(child) {
                    console.log(child.key+": "+child.val());
                  });
                });

更新:这是整个函数,现在它只是超时而没有响应。

'use strict';

// Firebase Functions
const functions = require('firebase-functions');
// Firebase Admin
const admin = require('firebase-admin');


// Default admin firebase configuration
admin.initializeApp(functions.config().firebase);

const rp = require('request-promise');
const promisePool = require('es6-promise-pool');
const PromisePool = promisePool.PromisePool;
const secureCompare = require('secure-compare');
const MAX_CONCURRENT = 3;

//Initial function call:
exports.CheckItemTypeinFB = functions.https.onRequest((req, res) => {


const key = req.query.key;


// Exit if the keys don't match
if (!secureCompare(key, functions.config().cron.key)) {
 console.log('The key provided in the request does not match the key set in    the environment. Check that', key,
    'matches the cron.key attribute in `firebase env:get`');
res.status(403).send('Security key does not match. Make sure your "key" URL query parameter matches the ' +
    'cron.key environment variable.');
return;
}



  // Try the database here...

  let userID = 'xxx';
  let ikey = 'xxxxx
  //create database ref
  let ref = admin.database().ref(`/items/${userID}/${ikey}`);
                //do a bunch of stuff



  ref.once("value", function(snapshot) {
        snapshot.forEach(function(child) {
            console.log(`${child.key}: ${child.val()}`);
        });
        res.send(200, {/* response data */});
    });




  //send back response
  // res.redirect(200);


})  // END THE MAJJOR CONTAINER THINGS


 //  Returns an access token using the Google Cloud metadata server. */
function getAccessToken(accessToken) {
 // If we have an accessToken in cache to re-use we pass it directly.
  if (accessToken) {
    return Promise.resolve(accessToken);
 }

  const options = {
   uri: 'http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token',
  headers: {'Metadata-Flavor': 'Google'},
  json: true
 };

  return rp(options).then(resp => resp.access_token);

}

非常感谢您的帮助。

更新:。超时是固定的,它返回数据库中“/items”下的用户 ID。但是,如果我使用 ${userId}/${key} 我什么也得不到。我仍然无法告诉如何让孩子在数据库中的随机 userId 下,并且我读过的其他帖子都没有解释它。 Firebase 的文档声明使用 {userId} 来获取该通配符下的所有内容,但它不起作用。我错过了什么?

【问题讨论】:

    标签: firebase firebase-realtime-database google-cloud-functions


    【解决方案1】:

    您没有返回 once 函数的结果或根本没有返回,因此该函数不知道何时完成因此超时。

    let userID = 'xxxxxxxxx';
    let key = 'xxxxxxxx';
    let ref = admin.database().ref(`/items/${userID}/${key}`);
    
    return ref.once("value", function(snapshot) {
        snapshot.forEach(function(child) {
            console.log(`${child.key}: ${child.val()}`);
        });
    });
    

    另外请注意,您正在观察的引用将为您提供特定用户项的子项(itemNameitemDateitemType)。如果您想要属于特定用户的项目,请将您的参考路径调整为 /items/${userID}

    在 HTTP 触发器中,您可以在观察值后发送响应。

    exports.CheckItemTypeinFB = functions.https.onRequest((req, res) => {
        ...
        ref.once("value", function(snapshot) {
            snapshot.forEach(function(child) {
                console.log(`${child.key}: ${child.val()}`);
            });
            res.send(200, {/* response data */});
        });
    });
    

    【讨论】:

    • 谢谢!不过,当我阅读有关 {wildcards} 的内容时,我一定是误解了。它仍然必须定义为默认值,然后替换为数据库中实际存在的任何内容?我还将键定义为: const key = req.query.key;
    • 另外,使用上面的答案,我在控制台没有响应,它仍然超时。
    • 好的,所以您在 HTTP 触发器中,因此您需要使用 res 做出响应,例如,res.send(200)
    • 啊,我不是故意要评论的,但未评论的结果相同....超时没有结果
    • 你还回来吗?
    猜你喜欢
    • 2020-04-15
    • 2017-09-21
    • 1970-01-01
    • 2021-11-01
    • 1970-01-01
    • 2019-12-03
    • 2018-11-30
    • 1970-01-01
    • 2019-09-21
    相关资源
    最近更新 更多