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
最满意答案
基本上这种情况正在发生,因为你将数字排序为字符串,所以它以这种方式给出结果(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; }];
相关问答
更多-
您的属性似乎是NSString类型。 确保你将它设置为NSNumber ,然后排序将按预期工作。 不需要在排序描述符中selector:@selector(compare:) ,因为这是默认值。 It seems your attribute is of type NSString. Make sure you make it to NSNumber, the sorting will then work as expected. There is no need to selector:@selector ...
-
NSSortDescriptor问题与排序单位和双位数字(NSSortDescriptor issue with sorting single and double digits number)[2023-12-13]
基本上这种情况正在发生,因为你将数字排序为字符串,所以它以这种方式给出结果(3> 11表示它只比较第一个数字,如果第一个数字相同然后比较下一个数字),你不需要改变数字但是根据数字而不是字符串对它们进行排序。 为此你应该使用 sortexp = [NSSortDescriptor sortDescriptorWithKey: @"Experience" ascending:YES selector:@selector(localizedStandardCompare:)]; 它会将您的数字与数字和字符串作为 ... -
获得一些数字(Getting a number of digits)[2021-09-07]
"%.3f" % math.pi 我知道它使用旧的语法,但我个人更喜欢它。 "%.3f" % math.pi I know its using the old syntax but I personally prefer it. -
你面临的问题是因为首先你应该知道float有4个字节而double有8个字节,3.141528579238也许可以存储在两者中如果你想显示更多数字我建议先包括: #include
然后你可以使用以下内容在“。”之后打印任意数量的数字。 : cout << std::setprecision(7) << myFloat; 这将显示3.1415285。 The problem that you are facing is because first you should know that f ... -
枚举数字(Enumerating digits)[2023-06-05]
你为什么要枚举数字? 存在枚举以给数字命名。 如果要存储数字,请存储整数 - 或者在本例中为char(因为您还需要存储字符)。 要进行验证,请接受一个字符串并编写如下函数: bool ISBN_Validate(string val) { if (!val.length ==) return false; if (val[0] < '0' || val[0] > '9') return false; foreach (char ch in val) ... -
function leadingZero(num : Number) : String { if(num < 10) { return "0" + num; } return num.toString(); } function leadingZero(num : Number) : String { if(num < 10) { return "0" + num; } return num.toString(); }
-
如何将数字格式化为小数点前的两位数字(How to format a number to always be double digits before the decimal)[2021-09-26]
使用("0" + number).slice(-2)格式化数字 尝试下面的代码片段 // Set the date we're counting down to var countDownDate = new Date("March 27, 2019 00:00:00").getTime(); // Update the count down every 1 second var x = setInterval(function() { // Get todays date ... -
用一个调试器来看转换过程,我认为会发生的事情是,double首先被转换为一个字符串,而忽略了精度。 然后,调整字符串以匹配指定的精度(在末尾填充0,或者舍入为舍入 - 请参阅apply.Precision of sun.misc.FormattedFloatingDecimal)。 而且,如果重新分析,原始转换似乎使用(最小?)小数位数来生成原始双精度值。 Looking at the conversion process with a debugger, I think what happens is t ...
-
要尽量减少手头问题的示例,请考虑以下事项: