1

I trying to write node function to call third party API . I using the angular Fire Function for display the results in angular project. The issues is no data response;

Here is my node js code.

const request = require('request');
const UserDetail =  () => {

    const options ={
        url: 'https://www.reddit.com/r/funny.json',
        method: 'GET',
        headers: {
            'Accept': 'application/json',
            'Accept-Charset': 'utf-8',
            'User-Agent': 'my-reddit-client'
        }
    }

    request(options, function(err, res, body) {
        let json = JSON.parse(body);
        console.log(json);
        
    });
     
}
UserDetail();

Here is my firebase function code:

exports.userdetails = functions.https.onRequest(require('./api/user/userdetail'));

Here is my angular service calling firebase function code:

callUserDetails(){
         const details = this.functions.httpsCallable('userdetails')({ text: 'Some Request Data' })
         .pipe()
         .subscribe(resp => {
           console.log({ resp });
         }, err => {
           console.error({ err });
         });
          
  }
2
  • In your UserDetail (), do you return anything from your request? Commented Apr 22, 2021 at 3:01
  • yes have return value when i run in node js no problems but after i deploy to firebase run in angular have problems. Commented Apr 22, 2021 at 3:35

2 Answers 2

1

You are mixing up Callable Cloud Functions and HTTPS Cloud Functions.

By doing

exports.userdetails = functions.https.onRequest(...)

you define an HTTPS Cloud Function,

but by doing

this.functions.httpsCallable('userdetails')({ text: 'Some Request Data' })

in your front-end, you actually call a Callable Cloud Function.


You should either change your Cloud Function to a Callable one, or call the userdetails HTTPS Cloud Function by sending an HTTP Request to the Cloud Function URL.

I would advise the first approach because Callable brings several advantages over a "simmple" HTTPS one (see the doc).


In addition you need to note that request supports callback interfaces natively but does not return a Promise. And it is necessary to use Promises in order to manage the life cycle of a Callable Cloud Function (see the official video serie).

I would use Axios along the following lines (untested):

exports.userdetails = functions.https.onCall(async (data, context) => {

    try {

        const options = {
            url: 'https://www.reddit.com/r/funny.json',
            method: 'get',
            headers: {
                'Accept': 'application/json',
                'Accept-Charset': 'utf-8',
                'User-Agent': 'my-reddit-client'
            }
        }

        const axiosResponse = await axios(options);

        // Build the resp to be sent to the frontend by 
        // using axiosResponse.data .... up to you, see https://github.com/axios/axios#response-schema

        return { resp: .... }

    } catch (error) {
        // See https://firebase.google.com/docs/functions/callable#handle_errors
    }

});
Sign up to request clarification or add additional context in comments.

Comments

0

Maybe you should call angular service like this:

// provider class
constructor(private http: HttpClient) {}

this.http.get(url, {params: {}, headers: {}}).subscribe(result => {
  //// result 
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.