Quantcast
Channel: Hot Weekly Questions - Web Applications Stack Exchange
Viewing all articles
Browse latest Browse all 9843

How to handle multiple server responses in one client request (NODE)?

$
0
0

Hi I'm in a situation where I don't know what is the right/best approach I'm looking for. This the sequence of events I'm looking for:

User enters some data in a FORM to find a jobOn submit -> data will be passed to NODE.Calculate how many results are found and inform the user about how much time to wait.NODE still processing the same request to identify job location, salary etc.Once done with all results -> reload page and display markers on map.The issue I'm currently facing is that I cannot get 2 responses on one request. I'm aware that this is impossible so I tried changing it to two different request, but I have no clue how to pass data from one function to another. Obviously making global variables is not the most efficient way of doing it as I'm dealing with more that 25 variables.

I found the following SO question rather interesting but in my case it didn't work or I have done it wrong. I have also tried using next() but it messes up my for loop. I have also tried setting my flash messages on res.locals but also didn't work or I have done it wrong. I have also tried with redirect instead of render and the error remains.

The whole thing is really confusing and I have no clue how to proceed. I managed to get my program to work up until STEP 4, but I get the following error:

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client    at ServerResponse.setHeader (_http_outgoing.js:518:11)    at ServerResponse.header (C:\inetpub\wwwroot\mymindmapper\node_modules\express\lib\response.js:771:10)    at ServerResponse.send (C:\inetpub\wwwroot\mymindmapper\node_modules\express\lib\response.js:170:12)    at done (C:\inetpub\wwwroot\mymindmapper\node_modules\express\lib\response.js:1008:10)    at tryHandleCache (C:\inetpub\wwwroot\mymindmapper\node_modules\ejs\lib\ejs.js:278:5)    at View.exports.renderFile [as engine] (C:\inetpub\wwwroot\mymindmapper\node_modules\ejs\lib\ejs.js:489:10)    at View.render (C:\inetpub\wwwroot\mymindmapper\node_modules\express\lib\view.js:135:8)    at tryRender (C:\inetpub\wwwroot\mymindmapper\node_modules\express\lib\application.js:640:10)    at Function.render (C:\inetpub\wwwroot\mymindmapper\node_modules\express\lib\application.js:592:3)    at ServerResponse.render (C:\inetpub\wwwroot\mymindmapper\node_modules\express\lib\response.js:1012:7)    at C:\inetpub\wwwroot\mymindmapper\routes\findJob.js:187:28

This is my code:

router.get('/getJob', (req, res) => {    return res.render('dashboardPages/jobSearchEngine', {        data: {             jobLocations: listOfJobLocations,            message: req.flash('message_handler')        }    });});router.post('/setJobFields', (req, res) => {    let numOfPages; //Number of pages according to the number of jobs found!    //get user input     let job_title = req.body.job_title;    //...    const options = {        //...    }    rp(options).then( (data) => { //FIND NUMBER OF PAGES        listOfJobIds = []; //CLEAR LIST OF JOB ID's        listOfJobLocations = []; //CLEAR LIST OF JOB LOCATIONS        numOfPages = getNumberOfPagesToLoop(data); //Get Number of PAGES according to number of jobs found!        rp(options).then( (data) => { //COLLECT ALL THE JOB ID's            var jobCounter = 1;            for(let i=0; i<numOfPages; i++){ //Loop thorugh number of pages                //Loop through results and get all ids                for(let j=0; j<50; j++){ //50 results per page                    if(jobCounter <= data.numberRecords){                        listOfJobIds.push(data.jvs[j].id);                        }else{                        console.log("    > ALL JOB ID's HAVE BEEN SAVED!");                        break;                    }                    jobCounter++;                }            }            req.flash('message_handler', {                type: 'info',                intro: 'PROCESSING :  ',                msg: 'Your request have been submitted. We have found '+ (jobCounter-1) +' jobs. Please wait X SECONDS while we process your request!'            });            res.render('dashboardPages/jobSearchEngine',                 {                    data: {                         jobLocations: listOfJobLocations,                        message: req.flash('message_handler')                    }                }            );            loopJobs(listOfJobIds).then( () => { //COLLECT JOB INFORMATION FOR ALL THE LISTED ID's                req.flash('message_handler', {                    type: 'success',                    intro: 'DONE :  ',                    msg: 'See the map below and click the markers to reveal the information about each job!'                });                console.log("\n\n\n" + listOfJobLocations);                return res.render('dashboardPages/jobSearchEngine', //ERROR HERE                    {                        data: {                             jobLocations: listOfJobLocations,                            message: req.flash('message_handler')                        }                    }                );            }).catch( (err) => {                console.log(err);            });        }).catch((err) => {            console.log(err);        });        }).catch( (err) => {        console.log(err);    });  });

Again, I understand that ONE client request should receive ONE server response back. However, I'm stuck and have no clue how to proceed. Most answers from the questions I have read confused me more.

The only easy solution I can think of is to pass the flash-message and somehow display it without calling render or redirect or send or json. Any help or guidance will be much appreciated, thanks!


Viewing all articles
Browse latest Browse all 9843

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>