0

I am trying to deploy my first function, but I am stuck, I don t understand what is wrong in my code. This is the error that I receive

functions[sendNotifications(europe-west1)]: Deployment error. 
Function failed on loading user code. Error message: Error: please examine your function logs 
to see the error cause: https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs

Even in that link I did not find something that could help me understand where is the problem, what I am doing wrong. I hope someone of you guys can help me, here it is my code:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const request = require('request');
const async = require('async');
admin.initializeApp(functions.config().functions);

exports.sendNotifications = functions
.region('europe-west1')
.pubsub.schedule('every day 13:00').onRun(async context => {

async.waterfall([
    function(callback) {
        url = 'http://niksimoni.pythonanywhere.com/api/pulizie_domani';
        request(url, (error, response, body) => {
            if (!error && response.statusCode === 200) {
                const APIResponse = JSON.parse(body);
                callback(null, APIResponse.strade);
            }
            else {
                callback('Error parsing the data');
            }
        })
    },
    function(streets, callback) {
        for (var i = 0; i < streets.length; i++) {
            async.waterfall([
                function(callback) {
                    let street = streets[i];
                    url = 'http://niksimoni.pythonanywhere.com/api/data_pulizie?indirizzo=' + street;
                    request(url, (error, response, body) => {
                        if (!error && response.statusCode === 200) {
                            const APIResponse = JSON.parse(body);
                            var topic = street;
                            var message = {
                                data: {
                                    title: street,
                                    subtitle: 'Pulizia domani alle ' + APIResponse.ora,
                                },
                                topic: topic
                            };
                            admin.messaging().send(message)
                            .then((response) => {
                                // Response is a message ID string.
                                console.log('Successfully sent message:', response);
                            })
                            .catch((error) => {
                                console.log('Error sending message:', error);
                            });
                            callback(null, message);
                        } else {
                            callback('Error parsing the data');
                        }
                    })
                }
            ], function(error, results) {
                if (error) {
                    console.log(error.message);
                }
            });
        }
        callback(null);
    }
], function(error, results) {
    if (error) {
        console.log(error.message);
    } 
});
});

1 Answer 1

1

Solved, I had to add the dependencies to the packages.json file

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

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.