为什么我的所有firebase云功能都超时了?

rek*_*irt 2 express firebase google-cloud-functions

我正在尝试使用firebase云功能来创建外部json api的代理.但是现在我只是想把它全部设置好.

我写了这个函数:

exports.helloWorld = functions.https.onRequest((request, response) => {
  request.get('http://www.google.com', function (error, response, body) {
    if (!error && response.statusCode == 200) {
      console.log(body) // Print the google web page.
    }
  })
});
Run Code Online (Sandbox Code Playgroud)

然后我运行firebase函数模拟器并运行

curl http://localhost:5000/<project-id>/us-central1/helloWorld
Run Code Online (Sandbox Code Playgroud)

它返回一条消息,说明函数已被触发,开始执行,但随后它就在那里旋转并旋转直到最终超时.

{"error":{"code":500,"status":"INTERNAL","message":"function execution attempt timed out"}}
Run Code Online (Sandbox Code Playgroud)

我不确定我做错了什么.

........

编辑

这个功能很完美:

exports.helloWorld = functions.https.onRequest((request, response) => {
  response.send('test');
})
Run Code Online (Sandbox Code Playgroud)

Dou*_*son 7

使用云功能,HTTPS类型功能必须将结果写入客户端以指示功能已完成执行.在写入结果之前,假定该函数仍在运行异步工作.

因此,当您的请求完成后,您应该发送一些响应,即使它是空的.不幸的是,你已经response用另一个对象隐藏了你的主要对象,所以你应该重命名其中一个:

exports.helloWorld = functions.https.onRequest((request, response) => {
  request.get('http://www.google.com', function (error, res, body) {
    if (!error && res.statusCode == 200) {
      console.log(body) // Print the google web page.
    }
    return response.send("") // this terminates the function
  })
})
Run Code Online (Sandbox Code Playgroud)