Bryansq2nt
Bryansq2nt

Reputation: 394

Google cloud function run for ever

hello everybody i am trying to trigger a cloud function with Google Cloud Scheduler but when i run the job this run for ever.

i have a realtime database with users data they have 2 booleans and when i run the cloud function this booleans change to false it is okay but the function never stop when i change again to true immediately change to false and i dont know why . this is my cron configuration 0 1 * * * and this is my code :

'use strict';
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

var db = admin.database();
exports.DailyResets = functions.https.onRequest((req, res) => {

    var usersRef = db.ref("users");
    return usersRef.orderByValue().on("value", function (snapshot) {
        snapshot.forEach(function (data) {
            console.log("Usuario: " + data.val().nombre + " Cupon" + data.val().Cupon);
            var hopperRef = usersRef.child(data.key.toString());
            hopperRef.update({
                "Cupon": false,
                "DailyGift": false
            });

        });
    });


});

my function its ok but run forever

Upvotes: 0

Views: 304

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598708

The on() method doesn't return a promise. You're probably looking for once():

exports.DailyResets = functions.https.onRequest((req, res) => {

    var usersRef = db.ref("users");
    return usersRef.orderByValue().once("value", function (snapshot) {
        snapshot.forEach(function (data) {
            console.log("Usuario: " + data.val().nombre + " Cupon" + data.val().Cupon);
            var hopperRef = usersRef.child(data.key.toString());
            return hopperRef.update({
                "Cupon": false,
                "DailyGift": false
            });
        });
    });
});

You'll note I also added a return on the call to hopperRef.update( as otherwise the function may end before the database is updated.

Upvotes: 2

Related Questions