用于发送通知的Firebase https云函数始终以超时结束
用于发送通知的Firebase https云函数始终以超时结束,firebase,google-cloud-functions,Firebase,Google Cloud Functions,函数执行耗时60002毫秒,状态为“超时” 我有一个云功能,可以收集用户的产品和价格数据,点击外部API获取最新价格,并在价格发生变化时发送通知。我读过几个类似的问题,比如,但实际上我正在向客户发送回复。我已尝试将超时时间增加到120秒,但仍然超时。所以,我认为我如何回报承诺可能会有问题?有什么建议吗 这是我的密码: exports.pushTestWithRP = functions.https.onRequest((req, res) => { var uidsAndTokens =
函数执行耗时60002毫秒,状态为“超时”
我有一个云功能,可以收集用户的产品和价格数据,点击外部API获取最新价格,并在价格发生变化时发送通知。我读过几个类似的问题,比如,但实际上我正在向客户发送回复。我已尝试将超时时间增加到120秒,但仍然超时。所以,我认为我如何回报承诺可能会有问题?有什么建议吗
这是我的密码:
exports.pushTestWithRP = functions.https.onRequest((req, res) => {
var uidsAndTokens = [];
ref.child('tokens').once('value').then(snap => {
snap.forEach(childSnap => {
uidsAndTokens.push({
uid: childSnap.key,
deviceToken: childSnap.val()
});
});
return uidsAndTokens;
}).then((uidsAndTokens) => {
var uidsAndCruises = [];
ref.child('watchlist-items/users').once('value').then(snap => {
snap.forEach(childSnap => {
var uid = childSnap.key;
childSnap.forEach(childChildSnap => {
var product = childChildSnap.key;
var productWatchInfo = childChildSnap.val();
uidsAndProducts.push({
uid: uid,
product: product,
watchInfo: productWatchInfo
});
}); // end childChildSnap
}); // end childSnap
return uidsAndProducts;
}).then((uidsAndProducts) => { // end snap watchlist-items/users
var uidsOnly = [];
for (var i=0; i<uidsAndTokens.length; i++) {
uidsOnly.push(uidsAndTokens[i].uid);
}
// user has a FCM token
var uidsAndProductsWithTokens = [];
for (var i=0; i<uidsAndProducts.length; i++) {
//check if exists in tokens array
var currUid = uidsAndProducts[i].uid;
if (uidsOnly.includes(currUid)) {
//console.log('this uid has a token: ', currUid);
uidsAndProductsWithTokens.push(uidsAndProducts[i]);
} else {
//this uid does NOT have a token
}
}
function getTokenForUid(uid) {
for (var i in uidsAndTokens) {
if (uidsAndTokens[i].uid == uid) {
var deviceToken = uidsAndTokens[i].deviceToken;
break;
}
}
return deviceToken;
}
var allPromises = [];
// call API only for uids with tokens
for (var i=0; i<uidsAndProductsWithTokens.length; i++) {
const product = uidsAndProductsWithTokens[i].product;
const uid = uidsAndProductsWithTokens[i].uid;
const deviceToken = getTokenForUid(uid);
const pDates = uidsAndProductsWithTokens[i].watchInfo.pDates;
const pName = uidsAndProductsWithTokens[i].watchInfo.pName;
getCurrentPricesFromAPI(product).then((response) => {
if (typeof response.response != 'undefined') {
const productId = response.response.product.product_id;
const allPrices = response.response.prices;
const promises = [];
// parse thru prices and send notifications
for (var date in pDates) {
// get all current prices and sort by price to get cheapest
var cheapest = [];
for (var i = 0; i < allPrices.length; i++) {
if (allPrices[i].data[productId][date] && allPrices[i].data[productId][date].hasOwnProperty('Inside')) {
const iPrice = allPrices[i].data[productId][date].Inside;
cheapest.push(iPrice);
}
}
if (cheapest[0] > 0) {
cheapest = cheapest.sort(function (a, b) { return a - b; });
if (sDates[date].hasOwnProperty('Inside')) {
const priceDiff = cheapest[0] - sDates[date].Inside.price;
if (priceDiff < -10) {
const payload = {
notification: {
title: pName + ' Price DROP Alert!',
body: 'prices for the ' + date + ' are $' + cheapest[0] + ' (DOWN $' + Math.abs(priceDiff) + ')',
sound: 'default'
}
};
promises.push(admin.messaging().sendToDevice(deviceToken, payload));
}
else if (priceDiff > 10) {
const payload = {
notification: {
title: pName + ' Price Hike Alert',
body: 'prices for the ' + date + ' are $' + cheapest[0] + ' (UP $' + priceDiff + ')',
sound: 'default'
}
};
promises.push(admin.messaging().sendToDevice(deviceToken, payload));
}
}
}
}
allPromises = allPromises.concat(promises);
}
}) // end handle API response
} // end for loop
return allPromises;
}).then((allPromises) => {
return Promise.all(allPromises);
res.send('got tokens and ids');
}).catch(error => {
res.send('there was an error');
});
}); // end uidsAndTokens
}); // end function
exports.pushTestWithRP=functions.https.onRequest((req,res)=>{
var uidsAndTokens=[];
ref.child('tokens')。一次('value')。然后(snap=>{
snap.forEach(childSnap=>{
uidsAndTokens.push({
uid:childSnap.key,
deviceToken:childSnap.val()
});
});
返回uidsAndTokens;
}).然后((uidsAndTokens)=>{
var uidsAndCruises=[];
ref.child('watchlist-items/users')。一次('value')。然后(snap=>{
snap.forEach(childSnap=>{
var uid=childSnap.key;
forEach(childSnap=>{
var product=childsnap.key;
var productWatchInfo=childChildSnap.val();
uidsAndProducts.push({
uid:uid,
产品:产品,,
watchInfo:productWatchInfo
});
});//结束childsnap
});//结束子快照
退回UID和产品;
}).then((uidsAndProducts)=>{//结束快照监视列表项/用户
var uidsOnly=[];
对于(变量i=0;变量i=10){
常数有效载荷={
通知:{
标题:pName+“涨价警报”,
正文:“+date+”的价格为$'+最便宜的[0]+'(上涨$'+priceDiff+)”,
声音:“默认”
}
};
promises.push(admin.messaging().sendToDevice(deviceToken,有效负载));
}
}
}
}
allPromises=allPromises.concat(承诺);
}
})//结束句柄API响应
}//循环结束
回报一切承诺;
}).然后((所有承诺)=>{
返回承诺。全部(所有承诺);
res.send('got tokens and id');
}).catch(错误=>{
res.send('有一个错误');
});
}); // 结束uidsAndTokens
}); // 端函数
我想不出来,如果有人能帮我,我将不胜感激