簡體   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