我将 Cloud Functions for Firebase 与 Firebase 实时数据库结合使用,以便为我的应用进行一些数据管理。
我的一个函数似乎被终止了,因为它需要大约 100-150 秒才能完成。发生这种情况时会出现错误:ESOCKETTIMEDOUT
。
有办法避免这种情况吗?
这是我的功能:
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 条目。
最佳答案
好吧,每次使用异步任务时都需要返回。
编辑:您返回“writeSuggestedCars”,但我认为它永远不会返回值。我没有编译器,但我认为它是 return 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)
});
}
});
}
关于node.js - 适用于 Firebase 的 ESOCKETTIMEDOUT 云函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46169527/