Firebase 云函数返回请求错误:错误:套接字挂起

Firebase 云函数返回请求错误:错误:套接字挂起,firebase,nodes,google-cloud-functions,onesignal,Firebase,Nodes,Google Cloud Functions,Onesignal,我已使用云功能连接onesignal服务,该服务将向用户发送通知。在本地测试函数后,它可以正常工作,但部署到云函数后,它会返回一个错误“RequestError:error:read ECONNRESET”,我希望云函数重置连接 下面是我的代码 exports.sendNotification = functions.pubsub.topic('cron-notification').onPublish(async (message) => { const databaseRef = ad

我已使用云功能连接onesignal服务,该服务将向用户发送通知。在本地测试函数后,它可以正常工作,但部署到云函数后,它会返回一个错误“RequestError:error:read ECONNRESET”,我希望云函数重置连接

下面是我的代码

exports.sendNotification = functions.pubsub.topic('cron-notification').onPublish(async (message) => {
const databaseRef = admin.database();
// Query all user from realtime db
const snapshotsUser = await databaseRef
    .ref(`user`)
    .orderByKey()
    .once("value");

// Check if user exist
if (snapshotsUser) {
    //Get the user key
    const user_object_key = Object.keys(snapshotsUser.val());

    // send notification for each user
    user_object_key.map(async (user_id) => {
        // query something
    const snapshotsUser = await databaseRef
    .ref(`record/user_id`)
    .orderByKey()
    .once("value");
        const message = {
            "app_id": "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxx",
            "filters": [
                {"field": "tag", "key": "user_id", "value": user_id}
            ],
            "headings":  {"en": `Hello World`},
            "contents": {"en": `Hello`}
        }
    sendNotification(message);
})}});

function sendNotification(message) {
const headers = {
    "Content-Type": "application/json; charset=utf-8",
    "Authorization": "Basic XXXXXXXXXXXXXXXXXXXXXXXXXXX"
};

// Use onesignal for send notification
const options = {
    uri: "https://onesignal.com/api/v1/notifications",
    headers: headers,
    method: 'POST',
    json: true,
    body: message,
    resolveWithFullResponse: true,
}

return request(options).then(response => {
    if (response.statusCode >= 400) {
        throw new Error(`HTTP Error: ${response.statusCode}`);
    } else {
        console.log(response.body)
    }
}).catch(error => {
    console.log(error);
})}

有人能给我建议吗?

根据对等方强制关闭连接时发生的“EconReset”错误。这会由于超时或重新启动而导致远程套接字上的连接丢失。由于您提到代码在本地工作,并且错误发生在部署之后,因此答案是,可能的解决方案是增加内核的数量,以便更快地为请求提供服务。另外,阅读GitHub关于错误的讨论可能会很有用。

对于那些与我面临相同问题的人,我只是注意到我没有在函数中返回任何内容。如果云函数没有返回任何内容,它将重置该函数。因此,从上面的代码应该是这样的,一切都很好

exports.sendNotification = functions.pubsub.topic('cron-notification').onPublish(async (message) => {
const databaseRef = admin.database();
// Query all user from realtime db
const snapshotsUser = await databaseRef
    .ref(`user`)
    .orderByKey()
    .once("value");

// Check if user exist
if (snapshotsUser) {
    //Get the user key
    const user_object_key = Object.keys(snapshotsUser.val());

    // send notification for each user
    return Promise.all([    user_object_key.map(async (user_id) => {
        // query something
    const snapshotsUser = await databaseRef
    .ref(`record/user_id`)
    .orderByKey()
    .once("value");
        const message = {
            "app_id": "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxx",
            "filters": [
                {"field": "tag", "key": "user_id", "value": user_id}
            ],
            "headings":  {"en": `Hello World`},
            "contents": {"en": `Hello`}
        }
    sendNotification(message);
})])}});

function sendNotification(message) {
const headers = {
    "Content-Type": "application/json; charset=utf-8",
    "Authorization": "Basic XXXXXXXXXXXXXXXXXXXXXXXXXXX"
};

// Use onesignal for send notification
const options = {
    uri: "https://onesignal.com/api/v1/notifications",
    headers: headers,
    method: 'POST',
    json: true,
    body: message,
    resolveWithFullResponse: true,
}

return request(options).then(response => {
    if (response.statusCode >= 400) {
        throw new Error(`HTTP Error: ${response.statusCode}`);
    } else {
        console.log(response.body)
    }
}).catch(error => {
    console.log(error);
})}