0

I have built a form to add some dummy data to a Mongo collection for testing purposes. The function to add the data seems to be working ok in that the data is being added ok, but, for some reason, the redirect command at the end of the function is not doing its thing. The data comes from a bog standard HTML form. Here is the code for adding the data:

router.post('/addacc',function(req,res){
    var sljEdate = "";
    var pljEdate = "";
    var MongoClient = mongodb.MongoClient;
    var url = 'mongodb://localhost:27017/stc';
    MongoClient.connect(url,function(err,db){
        if(err){
            console.log("Connection Error");
        }else{
            console.log("connected to mongo");
            var collection = db.collection('account');
            var query = {accountID: req.body.acc_id,companyName: req.body.acc_name,companyEmail:req.body.acc_email,companyPhone:req.body.acc_phone,companyPostCode:req.body.acc_postcode,
                        photographers: {phtID:"",phtName:"",phtPhone:"",phtEmail:"",phtLoc:"",preferredContact:"",lastJob:"",pljEpoch:pljEdate,phtNotes:""},
                        schools: {schoolID:"",schoolName:"",schoolPhone:"",schoolEmail:"",schoolPostCode:"",lastJob:"",sljEpoch:sljEdate,notes:""}};
            collection.insert(query,function(err,result){
                if(err){
                    console.log("Error inserting Account record",err);
                }else{
                    console.log("Data Insert Success");
                    res.redirect("/forms");
                }
                db.close();
            });
        }
    });
});

I added lines to log to the console in a few places so that I could check progress and they all fire, including the final 'Data Insert Success' one. The res.redirect which follows it, however, does nothing. In the browser the URL remains as '/addacc', though the node console shows a subsequent GET for the '/forms' URL.

I've tried a few variations for the redirect, using e.g. res.send("test") just to try and output something, also a res.render function, but none of them seem to work.

I'm pretty sure that I have missed something simple, but I've been staring at the code for too long and I can't see anything wrong with it. In fact, I've had nearly identical code working lots of times. The only thing I'm doing differently this time is that I'm inserting into a Mongo collection which includes embedded documents, but the data insert is not the problem as the data gets in fine.

Help please!

Drum
  • 505
  • 1
  • 5
  • 22
  • I think redirecting a POST request is a bit different. See: http://stackoverflow.com/questions/17612695/expressjs-how-to-redirect-a-post-request-with-parameters – MrWillihog Jul 27 '16 at 11:47
  • Ok, that's interesting and helpful, thanks. I'll have a bit more of a play and see if it gets me anywhere. – Drum Jul 27 '16 at 11:52

1 Answers1

0

Ok, it seems that the Node problem above was a bit of a red herring. The redirect is actually working ok (MrWillihog's comment above is duly noted for future reference anyway). I thought it must be as all the response codes on the npm console checked out fine. The problem is actually to do with the rendering of the Jade template. Specifically something to do with the the file layout.jade which I use to set out the HTML head. For some reason (yet to be determined) things work fine when calling the /forms page directly in the URL. The file forms.jade starts in the standard way with

extends layout

block content

With the opening BODY tag in layout.jade the page fails to load properly when called from the redirect: the layout.jade file renders (I put an H1 in there to check), but forms.jade doesn't.

I moved the BODY down in to forms.jade (under block.content) and it started working though, interestingly, the URL in the address bar remains as '/accadd'

Again I'm a bit stumped as I have used this pattern quite a few times with no issue. I'm guessing that I've just forgotten something simple. I'll keep digging until I find the answer, but at least now I should be looking in the right place.

Drum
  • 505
  • 1
  • 5
  • 22