Firebase的云功能

时间:2018-05-31 17:58:43

标签: firebase google-app-engine google-cloud-functions

我有一个由Pub / Sub事件触发的云功能。它使用request-promise从我用于数据的API发出多个GET请求。

它在Cloud Functions Emulator中本地运行时有效,但是在生产中我一直收到此错误:

{ RequestError: Error: socket hang up
    at new RequestError (/user_code/node_modules/request-promise/node_modules/request-promise-core/lib/errors.js:14:15)
    at Request.plumbing.callback (/user_code/node_modules/request-promise/node_modules/request-promise-core/lib/plumbing.js:87:29)
    at Request.RP$callback [as _callback] (/user_code/node_modules/request-promise/node_modules/request-promise-core/lib/plumbing.js:46:31)
    at self.callback (/user_code/node_modules/request/request.js:185:22)
    at emitOne (events.js:96:13)
    at Request.emit (events.js:188:7)
    at Request.onRequestError (/user_code/node_modules/request/request.js:877:8)
    at emitOne (events.js:96:13)
    at ClientRequest.emit (events.js:188:7)
    at TLSSocket.socketOnEnd (_http_client.js:346:9)
    at emitNone (events.js:91:20)
    at TLSSocket.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:80:11)
    at process._tickDomainCallback (internal/process/next_tick.js:128:9)
  name: 'RequestError',
  message: 'Error: socket hang up',
  cause: 
   { Error: socket hang up
       at createHangUpError (_http_client.js:254:15)
       at TLSSocket.socketOnEnd (_http_client.js:346:23)
       at emitNone (events.js:91:20)
       at TLSSocket.emit (events.js:185:7)
       at endReadableNT (_stream_readable.js:974:12)
       at _combinedTickCallback (internal/process/next_tick.js:80:11)
       at process._tickDomainCallback (internal/process/next_tick.js:128:9) code: 'ECONNRESET' },
  error: 
   { Error: socket hang up
       at createHangUpError (_http_client.js:254:15)
       at TLSSocket.socketOnEnd (_http_client.js:346:23)
       at emitNone (events.js:91:20)
       at TLSSocket.emit (events.js:185:7)
       at endReadableNT (_stream_readable.js:974:12)
       at _combinedTickCallback (internal/process/next_tick.js:80:11)
       at process._tickDomainCallback (internal/process/next_tick.js:128:9) code: 'ECONNRESET' },
  options: 
   { method: 'GET',
     uri: 'https://api.coinmarketcap.com/v2/ticker/?convert=BTC&start=0',
     json: true,
     callback: [Function: RP$callback],
     transform: undefined,
     simple: true,
     resolveWithFullResponse: false,
     transform2xxOnly: false },
  response: undefined }

我的云功能运行时,知道为什么我会收到此套接字挂起错误?

编辑:以下是我的云功能代码:

const functions = require('firebase-functions');
// Import Admin SDK
var admin = require("firebase-admin");
var reqprom = require('request-promise');

admin.initializeApp(functions.config().firebase);

exports.minutely_tick =
  functions.pubsub.topic('minutely-tick').onPublish((event) => {
    console.log('This job runs every 5 minutes!');
    getAllData()
      .then((dataArray) => {
        const ref = admin.database().ref(`myData/`);
        ref.set(dataArray);
        return console.log('dataArray length:', dataArray.length);
      })
      .catch((error) => {
        return console.log('Caught error: ', error);
      });
  });

function getAllData() {
  return new Promise((resolve, reject) => {

    var promises = [];
    var startInt = 0;
    for (i = 0; i < 1000; i++) {
      const options = {
        method: GET,
        uri: myUrlString,
        json: true
      }
      promises.push(reqprom(options));
      startInt += 100;
    }

    Promise.all(promises)
      .then((dict) => {
        var array = valuesFromDict(dict);
        return resolve(array);
      })
      .catch((error) => {
        return reject(error);
      });
  });

}

function valuesFromDict(dict) {
  var values = Object.keys(dict).map((key) => {
    return dict[key];
  });
  return values;
}

总之,该函数在循环中进行多个API调用。通过承诺,我等待所有人完成。然后将该数据格式化为我设置为Firebase数据库引用的数组。

2 个答案:

答案 0 :(得分:1)

您没有从您的承诺链中返回最终承诺。云函数要求您返回仅在函数中完成所有异步工作时才能解析的承诺。

return getAllData().then(...).catch(...)

答案 1 :(得分:0)

我的电脑上有一个内容过滤器,当我删除它时问题解决了