用于Firebase的ESOCKETTIMEDOUT云function

我正在将Firebase的云端函数与我的Firebase实时数据库一起使用,以便为我的应用程序执行一些数据pipe理。

我的一个function似乎终止了,因为它需要大约100-150秒才能完成。 这发生错误: ESOCKETTIMEDOUT

有没有办法来防止这一点?

这是我的function:

 function getTopCarsForUserWithPreferences(userId, genres) { const pathToCars = admin.database().ref('cars'); pathTocars.orderByChild("IsTop").equalTo(true).once("value").then(function(snapshot) { return writeSuggestedCars(userId, genres, snapshot); }).catch(reason => { console.log(reason) }) } function writeSuggestedCars(userId, genres, snapshot) { const carsToWrite = {}; var snapCount = 0 snapshot.forEach(function(carSnapshot) { snapCount += 1 const carDict = carSnapshot.val(); const carGenres = carDict.taCarGenre; const genre_one = genres[0]; const genre_two = genres[1]; if (carGenres[genre_one] === true ||carGenres[genre_two] == true) { carsToWrite[carSnapshot.key] = carDict } if (snapshot.numChildren() - 1 == snapCount) { const pathToSuggest = admin.database().ref('carsSuggested').child(userId); pathToSuggest.set(carsToWrite).then(snap => { }).catch(reason => { console.log(reason) }); } }); } 

getTopCarsForUserWithPreferences在用户添加首选项时被调用。 另外cars表有大约50k条目。

那么你每次使用asynchronous任务都需要返回。

编辑:你返回'writeSuggestedCars',但我认为它永远不会返回一个值。 我没有编译器,但我认为这是返回Promise.resolved()。 你能把它插入我放在“这里”吗?

也许这将工作:

 function getTopCarsForUserWithPreferences(userId, genres) { const pathToCars = admin.database().ref('cars'); return pathTocars.orderByChild("IsTop").equalTo(true).once("value").then(function(snapshot) { return writeSuggestedCars(userId, genres, snapshot); }).catch(reason => { console.log(reason) }) } function writeSuggestedCars(userId, genres, snapshot) { const carsToWrite = {}; var snapCount = 0 snapshot.forEach(function(carSnapshot) { snapCount += 1 const carDict = carSnapshot.val(); const carGenres = carDict.taCarGenre; const genre_one = genres[0]; const genre_two = genres[1]; if (carGenres[genre_one] === true ||carGenres[genre_two] == true) { carsToWrite[carSnapshot.key] = carDict } if (snapshot.numChildren() - 1 == snapCount) { const pathToSuggest = admin.database().ref('carsSuggested').child(userId); return pathToSuggest.set(carsToWrite).then(snap => { // 'HERE' I think return promise/Promise.resolve() will work }).catch(reason => { console.log(reason) }); } }); }