Victor Guijarro
Victor Guijarro

Reputation: 53

Cloud Functions working in emulator but not when deployed

I am trying to create a collection and a document once a document is created in Firebase.

It is working fine in emulator, but when I deploy my functions to Firebase project, they don't create that collection and document.

But if I create fields in the existing document (snapshot) it works.

In the function I get data from an API and write it to the new document (which is not being created at the moment).

Function:

exports.quoteEndPoint = functions.firestore.document('users/{userID}/followedStocks/{stockID}')
    .onCreate((snap, context) => {
        const stock_id = context.params.stockID;
        const user_id = context.params.userID;

        var request = require('request');
        var http = require('https');

        const options = {
            "method": "GET",
            "hostname": "alpha-vantage.p.rapidapi.com",
            "port": null,
            "path": '/query?function=GLOBAL_QUOTE&symbol='+stock_id+'&datatype=json',
            "headers": {
                "x-rapidapi-host": "%API_HOST%",
                "x-rapidapi-key": "%API_KEY%",
                "useQueryString": true

        }
    };


    const req = http.request(options, function(res){
        const chunks = [];
        res.on('data', function(chunk){
            chunks.push(chunk);
        });
        res.on('end', function(){
            const body = Buffer.concat(chunks);
            //console.log(body.toString());
            const result = JSON.parse(body.toString());
            console.log(result);

            //set values from json responso to Firebase
            return snap.ref.collection('quoteEndPoint').doc('data').set(
                {
                    'symbol': result['Global Quote']['01. symbol'],
                    'open': result['Global Quote']['02. open'],
                }, { merge: true }).then(()=>{
                    console.log('New quoteEndPoint fields for ' + stock_id + ' added to Firebase');
                })
                .catch(err => {
                    console.log(err);
                });             
        });
    })
    .on('error',(err) => {
        console.log('Error: '+err.message);
    });

    req.end();
    
    return true;

    });

I tried to make the function: function() async, but it didn't work.

In emulator is creating and populating values to the right path : /users/7nDGdHmZDuoDiJkxixgz/followedStocks/AMD/quoteEndPoint/data

Anyone can help?

Thanks

Upvotes: 1

Views: 1158

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83058

Your problem most probably comes from the fact that a call with request does not return a promise, while in Cloud Functions triggered by background events (like .onCreate() for Firestore) you must return a Promise. Watch this official video series for more details: in particular the 3 videos titled "Learn JavaScript Promises".

In addition, request is deprecated.

You can use axios, which returns a Promise, or node-fetch. You need to chain the promises returned by the asynchronous operations, i.e. axios and Firestore asynchronous calls, as illustrated in the following code "skeleton":

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

exports.quoteEndPoint = functions.firestore.document('users/{userID}/followedStocks/{stockID}')
    .onCreate((snap, context) => {
        
        const stock_id = context.params.stockID;
        const user_id = context.params.userID;

        return axios({
            method: 'get',
            url: 'http://....'
            // ... See the doc
        })
        .then(response => {
            // ...

            return snap.ref.collection('quoteEndPoint').doc('data').set(...);
        });

    });

Upvotes: 2

Related Questions