0

I am trying to access Firebase Database using REST API. Using cURL i am able to retrive data. I am unable to do the same in javascript.

cURL

curl 'https://testproject123.firebaseio.com/subscription.json?auth=aabbccddeeff123'

Javascript

var request = require('request');

var options = {
    url: 'https://testproject123.firebaseio.com/subscription.json?auth=aabbccddeeff123' };

function callback(error, response, body) {
    if (!error && response.statusCode == 200) {
        console.log(body);
    } }

request(options, callback);

Note: The above code i am trying to run on Bluemix/OpenWhisk. Kindly let me know how to fix this.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Kanan Jarrus
  • 607
  • 1
  • 12
  • 26
  • 3
    What makes you think there is a problem? Are any errors reported? Your callback function takes an error argument, but the only thing you do with it is check that it is false. Have you considered actually looking at its value if it isn't false? – Quentin Jul 13 '16 at 07:06
  • @Quentin `var request = require('request'); var options = { url: 'https://docs-examples.firebaseio.com/rest/quickstart/users.json' }; function callback(error, response, body) { if (!error && response.statusCode == 200) { console.log(body); console.log(response); } else{ console.log(error);}} request(options, callback);` – Kanan Jarrus Jul 13 '16 at 07:26
  • @Quentin Tried the above code in jsfidlde. it does not print anything. But when you run `curl 'https://docs-examples.firebaseio.com/rest/quickstart/users.json'` i get an output `{"alanisawesome":{"birthday":"June 23, 1912","name":"Alan Turing"}}` That url is from firebase example which does not require auth. – Kanan Jarrus Jul 13 '16 at 07:27

1 Answers1

4

The structure of your action is a bit off for OpenWhisk. Javascript actions need to have a main method and you'll have to use whisk.async() and whisk.done() to make your (asynchronous) REST API call work as you'd expect it.

An example of how to call an external API using a Javascript action in OpenWhisk can be found here.

markusthoemmes
  • 3,080
  • 14
  • 23