2017-09-22 87 views
0

我試圖使用firebase雲功能來創建一個外部json API的代理。但現在我只是想把它全部建立起來。爲什麼我所有的firebase雲端功能都會超時?

我寫了這個功能:

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. 
    } 
    }) 
}); 

我然後運行火力功能仿真器和運行

curl http://localhost:5000/<project-id>/us-central1/helloWorld 

它返回一個消息,說的功能被觸發,開始執行,但後來它只是坐在在那裏和旋轉,直到最終超時。

{"error":{"code":500,"status":"INTERNAL","message":"function execution attempt timed out"}} 

我不知道我在做什麼錯。

........

編輯

此功能完美的作品:

exports.helloWorld = functions.https.onRequest((request, response) => { 
    response.send('test'); 
}) 

回答

5

雲功能,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 
    }) 
}) 
+0

我複製並粘貼您的確切功能,它仍然超時,但我知道firebase設置是正確的,因爲如果我只是發送一個字符串的響應它的工作。請參閱我的編輯。 – reknirt

+0

我發送之前發回 - 這應該解決它。 –

+0

我很抱歉難以感謝您的幫助,但它仍然是超時,我認爲這可能是另一個陰影問題,但我也嘗試過這種變化,我也嘗試過除谷歌和其他類似的東西之外的其他url。是否有更好的方法來編寫函數? – reknirt

3

HTTPS功能不完整的,直到你發送的響應東西。下面是剛剛管道從作爲輸出的代理請求的內容(我不得不改變變量的名稱,以避免陰影的例子:

exports.helloWorld = functions.https.onRequest((req, res) => { 
    request.get('http://www.google.com', function (error, response, body) { 
    if (!error && response.statusCode == 200) { 
     return res.send(body) // Print the google web page. 
    } 
    return res.send('ERROR: ' + error.message); 
    }) 
}); 
相關問題