首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误:无法在以firebase云函数发送标头后设置它们

错误:无法在以firebase云函数发送标头后设置它们
EN

Stack Overflow用户
提问于 2017-08-06 14:16:56
回答 1查看 3.4K关注 0票数 10

我在我的安卓应用程序中使用Firebase数据库。我正在使用它的云功能特性。我有编码函数,并试图在本地机器上测试它。

我正在运行这个命令来测试它,云函数在firebase服务器上运行得很好,而不是在本地主机上。下面的命令发送一个错误

消防基地服务-只提供功能

代码语言:javascript
复制
> firebase serve --only functions

=== Serving from 'D:\Firebase'...

i  functions: Preparing to emulate HTTPS functions. Support for other event types coming soon.
Warning: You're using Node.js v7.10.0 but Google Cloud Functions only supports v6.11.1.
Server#addProtoService is deprecated. Use addService instead
+  functions: getUsers: http://localhost:5002/xyz/us-central1/getUsers
info: User function triggered, starting execution
info: Execution took 60022 ms, finished with status: 'timeout'
info: Execution took 60022 ms, finished with status: 'crash'
Error: Can't set headers after they are sent.
    at ServerResponse.setHeader (_http_outgoing.js:371:11)
    at ServerResponse.header (C:\Users\williams\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\response.js:730:10)
    at ServerResponse.send (C:\Users\williams\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\response.js:170:12)
    at ServerResponse.json (C:\Users\williams\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\response.js:256:15)
    at ProxyServer.Supervisor._proxy.on (C:\Users\williams\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\@google-cloud\functions-emulator\src\supervisor\supervisor.js:97:12)
    at ProxyServer.emit (C:\Users\williams\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\eventemitter3\index.js:144:27)
    at ClientRequest.proxyError (C:\Users\williams\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\http-proxy\lib\http-proxy\passes\web-incoming.js:156:18)
    at emitOne (events.js:96:13)
    at ClientRequest.emit (events.js:191:7)
    at Socket.socketErrorListener (_http_client.js:358:9)
info: Execution took 60054 ms, finished with status: 'crash'
Error: Can't set headers after they are sent.
    at ServerResponse.setHeader (_http_outgoing.js:371:11)
    at ServerResponse.header (C:\Users\williams\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\response.js:730:10)
    at ServerResponse.send (C:\Users\williams\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\response.js:170:12)
    at ServerResponse.json (C:\Users\williams\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\response.js:256:15)
    at ProxyServer.Supervisor._proxy.on (C:\Users\williams\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\@google-cloud\functions-emulator\src\supervisor\supervisor.js:97:12)
    at ProxyServer.emit (C:\Users\williams\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\eventemitter3\index.js:144:27)
    at ClientRequest.proxyError (C:\Users\williams\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\http-proxy\lib\http-proxy\passes\web-incoming.js:156:18)
    at emitOne (events.js:96:13)
    at ClientRequest.emit (events.js:191:7)
    at Socket.socketCloseListener (_http_client.js:334:9)

代码:

代码语言:javascript
复制
exports.getUsers = functions.https.onRequest((request, response) => {
  var x = [],
    similarityCount;

  db
    .ref("/users/" + request.query.userId)
    .once("value")
    .then(function(snapshot) {
      var jsonObject = snapshot.val();
      var basicProfileJsonObject = jsonObject.basicProfile;
      for (var key in basicProfileJsonObject) {
        if (utils.isNumber(basicProfileJsonObject[key])) {
          x.push(basicProfileJsonObject[key]);
        }
      }
      db.ref("/users/").once("value").then(function(snapshot) {
        var y = [];
        var jsonResponse = [];
        snapshot.forEach(function(item) {
          var user = item.val();
          let userId = user.basicProfile.userId;
          if (userId !== request.query.userId) {
            var basicProfileJsonObject = user.basicProfile;
            for (var key in basicProfileJsonObject) {
              if (utils.isNumber(basicProfileJsonObject[key])) {
                y.push(basicProfileJsonObject[key]);
              }
            }

            if (request.query.algo === "cosine") {
              // compute cosine value
              similarityCount = cosineUtils.cosineSimilarity(x, y);
            } else if (request.query.algo == "euclidean") {
              // compute euclidean distance value
              similarityCount = 1 / (1 + euclidean(x, y));
            } else if (request.query.algo === "pearson-correlation") {
              // compute pearson correlation coefficents
              similarityCount = pcorr.pearsonCorrelation(x, y);
            }
            console.log("------------------------------------");
            console.log(x);
            console.log("------------------------------------");
            console.log("------------------------------------");
            console.log(y);
            console.log("------------------------------------");
            console.log("------------------------------------");
            console.log(similarityCount);
            console.log("------------------------------------");
            jsonResponse.push(item.val());
            y = [];
          }
        });
        response.send(jsonResponse);
      });
    });
});

有人能帮我一下出什么事了吗?

EN

回答 1

Stack Overflow用户

发布于 2017-08-07 18:30:10

这绝对是个棘手的案子。我认为这里发生了一个复杂的错误情况,而错误信息并不普遍。注意,有两行写着“执行花费了60022 ms,完成时状态为: X”。我在这里猜测云函数超时(然后发送响应),但是您的代码继续在后台运行。最终,它试图发送真正的响应,但是响应已经发送,Express.js抛出了一个异常。

另外,这个云函数试图在HTTP请求限制期间加载应用程序中的每个用户。无论云函数或Firebase实时数据库获得的速度有多快,如果您试图在恒定超时中安装O(N)操作,就会遇到限制。是否可以让您的HTTP函数计算两个用户之间的相似性?还是主动计算和失效用户在背景中的相似之处,并返回HTTP函数中缓存的值?

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45532895

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档