1

내 애플 리케이션을위한 데이터 관리를 위해 파이어베이스 용 클라우드 기능을 파이어베이스 리얼 타임 데이터베이스와 함께 사용하고 있습니다.ESOCKETTIMEDOUT 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 테이블에는 약 50,000 개의 항목이 있습니다.

답변

1

비동기 작업을 사용할 때마다 반환해야합니다.

편집 : 당신은 '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) 
      }); 
     } 
    }); 
} 
관련 문제