0

My Code is this but it doesnt work;

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

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

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

});
Dave
  • 5,108
  • 16
  • 30
  • 40
  • Hi Engin and welcome to SO. Thanks for providing the code you've tried. Are you getting any errors or response from the function at all? If so, what is it? What about in the Firebase console? Are you sure that you're calling the cloud function properly? – JeremyW Nov 30 '18 at 13:10
  • Hi Jeremy. I tried some other functions before and they worked. I didnt get any error in my browser when i enter the link of the function above but i cant see any random number in my browser. – Engin Yılmaz Nov 30 '18 at 13:56

1 Answers1

2

From https://firebase.google.com/docs/functions/http-events#terminate_http_functions ,

Always end an HTTP function with send(), redirect(), or end()

So your function will be:

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

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

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

});

Update: From this SO thread, you can't send only numbers in express.

Try this, it will make it string and send (credit to 1):

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

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

response.send(''+randN);

});
hkchakladar
  • 749
  • 1
  • 7
  • 15
  • Hi hkchakladar. I tried your code but it didnt work. Maybe i need to put a textbox like thing to write an integer value to http page, huh? – Engin Yılmaz Nov 30 '18 at 14:26
  • Thank you so much. It worked but i have another question, why you put ' ' ? – Engin Yılmaz Nov 30 '18 at 15:00
  • `''` is an empty string and `+` is concatenation operator, which joins two strings. So joining `''`, a blank string to `randN` (a Number) makes the whole output as `String`. – hkchakladar Nov 30 '18 at 15:15
  • I tried without it like this; response.send(randN); and its also worked. Thank you again. – Engin Yılmaz Nov 30 '18 at 15:19