Firebase云端功能总是超时 [英] Firebase cloud function always timeout

查看:202
本文介绍了Firebase云端功能总是超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



问题是,即使我设法发送一个通知通知,请求总是超时。

这是我的脚本

  const functions = require('firebase-functions'); 
const admin = require('firebase-admin');

admin.initializeApp(functions.config()。firebase);

exports.friendRequestNotification = functions.https.onRequest((req,res)=> {

const senderId = req.query.senderId;
const recipientId = req.query.recipientId;
const getRecipientPromise = admin.database()。ref(`/ players / $ {recipientId}`).once('value');
const getSenderPromise = admin.database ().ref(`/ players / $ {senderId}`).once('value');

return Promise.all([getRecipientPromise,getSenderPromise])。then(results => {
$ b const const recipient = results [0];
const sender = results [1];
$ b $常量recipientToken = recipient.child(notificationsInfo / fcmToken)。 val();
const notificationAuthorization = recipient.child(notificationsInfo / wantsToReceiveNotifications).val();
const recipientBadge = recipient.child(notificationsInfo / badgeNumber).val();
const senderUsername = sender.child(username)。val();

const payload = {
通知:{
title:`FriendRequest`,
body:`您有来自$ {senderUsername}!`,
徽章的新的好友请求:(recipientBadge + 1).toString()
}
};

if(notificationAuthorization){

return admin.messaging()。sendToDevice(recipientToken,payload).then(response => {

});


$ b return admin.database()。ref(`/ players / $ {recipientId} / notificationsInfo / badgeNumber`).setValue(recipientBadge + 1);

});

});

另外看来badgeNumber从来没有更新过,是关系到超时问题吗?

解决方案

HTTP触发的云功能就像Express应用程序一样工作 - 您有一个响应对象( res ),当请求完成时,你需要使用它来发送一些东西。在这种情况下,它看起来像你可以做一些事情:

  return Promise.all([
/ *。 ()=> {
res.status(200).send('ok');
})。catch(err => {
console.log(err.stack);
res.status(500).send('error');
});


I'm exploring the firebase cloud functions and I'm trying to send a notifications with an http request.

The problem is that even if I manage to send the notification, the request always goes timeout.

Here's my script

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);

exports.friendRequestNotification = functions.https.onRequest((req, res) => {

    const senderId = req.query.senderId;
    const recipientId = req.query.recipientId;
    const getRecipientPromise = admin.database().ref(`/players/${recipientId}`).once('value');
    const getSenderPromise = admin.database().ref(`/players/${senderId}`).once('value');

    return Promise.all([getRecipientPromise, getSenderPromise]).then(results => {

        const recipient = results[0];
        const sender = results[1];

        const recipientToken = recipient.child("notificationsInfo/fcmToken").val();
        const notificationAuthorization = recipient.child("notificationsInfo/wantsToReceiveNotifications").val();
        const recipientBadge = recipient.child("notificationsInfo/badgeNumber").val();
        const senderUsername = sender.child("username").val();

        const payload = {
            notification: {
              title: `FriendRequest`,
              body: `You have a new friend request from ${senderUsername}!`,
              badge: (recipientBadge+1).toString()
            }
        };

        if (notificationAuthorization) {

            return admin.messaging().sendToDevice(recipientToken, payload).then(response => {

            });

        }

        return admin.database().ref(`/players/${recipientId}/notificationsInfo/badgeNumber`).setValue(recipientBadge+1);

    });

});

Plus It seems that the badgeNumber in never updated, is that related to the timeout issue?

解决方案

HTTP-triggered Cloud Functions work just like Express apps -- you have a response object (res) that you need to use to send something when the request is done. In this case, it looks like you could do something like:

return Promise.all([
  /* ... */
]).then(() => {
  res.status(200).send('ok');
}).catch(err => {
  console.log(err.stack);
  res.status(500).send('error');
});

这篇关于Firebase云端功能总是超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆