首页 \ 问答 \ Firebase的云功能突然超时(Cloud Functions for Firebase suddenly timing out)

Firebase的云功能突然超时(Cloud Functions for Firebase suddenly timing out)

在今天部署我的功能之后突然间,我们的一些功能突然总是超时。 我在云功能控制台中看到了这一点。 这似乎发生在嵌套和返回承诺时。 奇怪的是,这从未发生过。

这是我的代码。

exports.pushMessageQueue = functions.database.ref('/userPushMessagesQueue/{userId}/{messageId}').onWrite(event => {
    if (!event.data.exists()) {
        console.log('DOES NOT EXIST!');
        return "not exists";
    }

    console.log("EXISTS!");

    const userId = event.params['userId'];
    const messageId = event.params['messageId'];

    const payload = event.data.val();

    return database.ref('devices').orderByChild('user_id').equalTo(userId).once('value', snapshot => {
        if (!snapshot.exists()) {
            return "no devices found";
        }

        const devices = snapshot.val();

        const deviceTokens = [];

        snapshot.forEach(deviceSnap => {
            const device = deviceSnap.val();
            deviceTokens.push(device['fcm_key']);
        });

        const removeFromQueue = database.ref(`/userPushMessagesQueue/${userId}/${messageId}`);

        console.log('then0');

        return removeFromQueue.set(null).then(() => {
            console.log('then1');
            return admin.messaging().sendToDevice(deviceTokens, payload).then(() => {
                console.log('then2');
                return "send";
            });
        }, (error) => {
            console.log('error1!');
            console.log(error);
        });
    });
});

在控制台中,将记录then0,而不是then1然后是2。 当我收到推送通知时,该条目将从队列中删除。

有什么我做错了吗?


Suddenly after deploying my functions today some of our functions suddenly always timeout. I see this in the cloud functions console. This seems to happen when nesting and returning promises. Strange thing is this never happened before.

Here's my code.

exports.pushMessageQueue = functions.database.ref('/userPushMessagesQueue/{userId}/{messageId}').onWrite(event => {
    if (!event.data.exists()) {
        console.log('DOES NOT EXIST!');
        return "not exists";
    }

    console.log("EXISTS!");

    const userId = event.params['userId'];
    const messageId = event.params['messageId'];

    const payload = event.data.val();

    return database.ref('devices').orderByChild('user_id').equalTo(userId).once('value', snapshot => {
        if (!snapshot.exists()) {
            return "no devices found";
        }

        const devices = snapshot.val();

        const deviceTokens = [];

        snapshot.forEach(deviceSnap => {
            const device = deviceSnap.val();
            deviceTokens.push(device['fcm_key']);
        });

        const removeFromQueue = database.ref(`/userPushMessagesQueue/${userId}/${messageId}`);

        console.log('then0');

        return removeFromQueue.set(null).then(() => {
            console.log('then1');
            return admin.messaging().sendToDevice(deviceTokens, payload).then(() => {
                console.log('then2');
                return "send";
            });
        }, (error) => {
            console.log('error1!');
            console.log(error);
        });
    });
});

In the console then0 is logged, not then1 and then2. And that while I receive the push notification and the entry is removed from the queue.

Is there something I am doing wrong?


原文:https://stackoverflow.com/questions/43098952
更新时间:2022-06-13 18:06

最满意答案

基本上这种情况正在发生,因为你将数字排序为字符串,所以它以这种方式给出结果(3> 11表示它只比较第一个数字,如果第一个数字相同然后比较下一个数字),你不需要改变数字但是根据数字而不是字符串对它们进行排序。 为此你应该使用

sortexp = [NSSortDescriptor sortDescriptorWithKey: @"Experience" ascending:YES selector:@selector(localizedStandardCompare:)];

它会将您的数字与数字和字符串作为字符串进行比较。

编辑:

您也可以对使用块进行排序。

NSArray *sortedArray = [array sortedArrayUsingComparator: ^(id obj1, id obj2) {

 if ([[obj1 valueForKey:@"Experience"] integerValue] > [[obj2 valueForKey:@"Experience"] integerValue]) {
      return (NSComparisonResult)NSOrderedDescending;
 }

 if ([[obj1 valueForKey:@"Experience"] integerValue] < [[obj2 valueForKey:@"Experience"] integerValue]) {
      return (NSComparisonResult)NSOrderedAscending;
 }
 return (NSComparisonResult)NSOrderedSame;
}];

Basically this is happening because you are sorting your numbers as string so it gives result in such a way (3 > 11 means it compares only first digit and if first digit same then compare next one) , you do not need to change the digits but sort them according to numbers not as string. For this you should use

sortexp = [NSSortDescriptor sortDescriptorWithKey: @"Experience" ascending:YES selector:@selector(localizedStandardCompare:)];

It will compare your numbers as number and string as string.

EDIT:

You can sort Using blocks as well.

NSArray *sortedArray = [array sortedArrayUsingComparator: ^(id obj1, id obj2) {

 if ([[obj1 valueForKey:@"Experience"] integerValue] > [[obj2 valueForKey:@"Experience"] integerValue]) {
      return (NSComparisonResult)NSOrderedDescending;
 }

 if ([[obj1 valueForKey:@"Experience"] integerValue] < [[obj2 valueForKey:@"Experience"] integerValue]) {
      return (NSComparisonResult)NSOrderedAscending;
 }
 return (NSComparisonResult)NSOrderedSame;
}];

相关问答

更多