Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

238
Vistas
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

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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(...);
        });

    });
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda