Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

239
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda