I am new in nodejs so forgive me if this question seemes stupide for you
when i test this function and both list are empty i get a timeout
=> when i dont have nothing to show and i need to return RmissionInProgress and SmissionInProgress empty
but when i have one item in any list , it works
=> one item in SmissionInProgress or RmissionInProgress => i have a returned result
app.get('/driver/missions/all', isAuthenticated, (req, res) => {
let language = usedLanguage(req.headers.language);
let token = req.headers.token;
let driverRef = req.body.driverRef;
let SmissionInProgress = [];
let RmissionInProgress = [];
let count = 0;
let driverId;
let bar = new Promise((resolve, reject) => {
db.collection('driverMissions').where('acceptedBy', '==', driverRef).get().then((snapshot) => {
if (snapshot.size > 0) {
snapshot.docs.forEach((doc) => {
doc.data().missionId.get().then((snap) => {
doc.data().idDriver.get().then((driverData) => {
let missionDetails = {
id: snap.id,
arrivalPosition: snap.data().arrivalPosition,
clientName: snap.data().clientName
}
RmissionInProgress.push(missionDetails);
if (count === snapshot.docs.length - 1) resolve();
count++;
})
});
});
}
}).catch((error) => {
console.log(error);
res.status(500).json({
message: languages.translate(language, "serverError")
});
});
db.collection('driverMissions').where('idDriver', '==', driverRef).get().then((snapshot) => {
console.log("done here 2");
if (snapshot.size > 0) {
snapshot.docs.forEach((doc) => {
doc.data().missionId.get().then((snap) => {
if ((["crée", "en attente", "expirée"].includes(snap.data().status)) || (snap.data().status === "annulée" && !doc.data().acceptedBy)) {
let missionDetails = {
id: snap.id,
arrivalPosition: snap.data().arrivalPosition,
clientName: snap.data().clientName,
}
SmissionInProgress.push(missionDetails);
}
});
});
}
}).catch((error) => {
console.log(error);
res.status(500).json({
message: languages.translate(language, "serverError")
});
});
}).catch((error) => {
console.log(error);
res.status(500).json({
message: languages.translate(language, "serverError")
});
});
});
bar.then(() => {
return res.status(200).json({
data: {
SmissionInProgress,
RmissionInProgress
}
});
});
});
question from:
https://stackoverflow.com/questions/65921826/timeout-in-app-get-function-in-nodjs-when-list-is-empty 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…