javascript - Firebase 云功能 - 如何解决此错误 : ECONNRESET

标签 javascript node.js firebase google-cloud-functions firebase-cloud-messaging

这是我的代码

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

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

最佳答案

始终返回云函数内的 promise :

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

还有这个:

return admin.messaging().send(message)

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

关于javascript - Firebase 云功能 - 如何解决此错误 : ECONNRESET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64303444/

相关文章:

javascript - 当值是reduce函数中的对象时访问属性(CouchDB)

node.js - 将 Mongoose 流转换为数组

node.js - 为什么子文件夹没有部署到 Google App Engine Nodejs 应用程序

javascript - 无法将数据从 React Js 发布到 Node js

ios - 在嵌套的子项中迭代。火力基地和 swift 3

android - java.lang.NoClassDefFoundError : com. 谷歌.android.gms.common.internal.zzbp

java - 检查 Firebase 数据库中存在的所有节点中的特定值

javascript - 贪吃蛇游戏游戏结束

javascript - 在组件端删除导入的 CSS

javascript - 从 JavaScript 数组中删除元素