繁体   English   中英

Firebase Cloud Function - 我该如何解决这个错误:ECONNRESET

[英]Firebase Cloud Function - How can I solve this error: ECONNRESET

这是我的代码

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

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

    axios.get('http://niksimoni.pythonanywhere.com/api/pulizie_domani')
    .then(listOfStreets => {
        const streets = listOfStreets.data.strade;
        const promises = [];
        for (const street of streets) {
            const p = axios.get('http://niksimoni.pythonanywhere.com/api/data_pulizie?indirizzo=' + street)
            .then(listOfHours => {
                const ora = listOfHours.data.ora;
                var topic = street.split(" ").join("-");
                var message = {
                    data: {
                        title: street,
                        subtitle: 'Pulizia domani alle ' + 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);
                });
            })
            promises.push(p);
        }
        return Promise.all(promises);
    })
    .catch(error => {
        console.log(error);
    });
  });

我尝试用请求替换 axios 但它没有解决问题,无论我更改什么我总是收到此错误:'发出请求时出错:Client.network 套接字在建立安全 TLS 连接之前断开连接。 错误代码:ECONNRESET' 我试着学习如何处理 promise,我以为我理解了,但也许我错了。 任何帮助将不胜感激

始终返回云函数内部的 promise:

return axios.get('http://niksimoni.pythonanywhere.com/api/pulizie_domani')

这也是:

return admin.messaging().send(message)

否则,云函数会突然终止正在执行的代码。 您可能也想为您的 catch 块做同样的事情。 您可以在这里阅读更多相关信息: https://firebase.google.com/docs/functions/terminate-functions

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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