如何在Firebase Cloud Functions中的HTTP调用中返回随机数?

时间:2018-11-30 12:49:50

标签: function firebase http random cloud

我的代码是这样,但是它不起作用;

const functions = require('firebase-functions');

exports.randomNumber = functions.https.onRequest((request, response) => {

return Math.floor(Math.random() * 20)

});

1 个答案:

答案 0 :(得分:1)

来自https://firebase.google.com/docs/functions/http-events#terminate_http_functions

  

始终使用send(),redirect()或end()结束HTTP函数

所以您的功能将是:

const functions = require('firebase-functions');

exports.randomNumber = functions.https.onRequest((request, response) => {

response.send(Math.floor(Math.random() * 20))

});

更新: 在此SO线程中,您不能只发送快速号码。

尝试一下,它将变成字符串并发送(贷记到1):

exports.randomNumber = functions.https.onRequest((request, response) => {

var randN = Math.floor(Math.random() * 20);

response.send(''+randN);

});