简体   繁体   中英

Firebase Cloud Function not deploying

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM