Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

node.js - Timeout in app.get function in nodjs when list is empty

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...