1

I have an Express post route which updates a mongo db based on data sent to it from a DataTables Editor inline field. So far so good and the database is updating fine. Where it falls over is after the update query executes successfully and I want to then redirect back to the originating page. The Node console seems to indicate that the GET route to the originating page is being called, but the page itself doesn't load.

The code for the POST function is as follows:

router.post('/edit', function(req,res){
var theKeys = _.keys(req.body);
var theMeat = theKeys[1];
var bits1 = theMeat.split("][");
// this is the updated value
var newVal = req.body[theMeat];
// this is the row _id 
var cleanId = bits1[0].replace("data[row_","");
// this is the field to update
var cleanRow = bits1[1].replace("]","");
// cast the id string back to an ObjectId
var updId = new ObjectId(cleanId);

var query = {};

query[cleanRow] = newVal;

var MongoClient = mongodb.MongoClient;
var url = 'mongodb://localhost:27017/gts';
MongoClient.connect(url, function(err, db){
    if(err){
        console.log("Error connecting to the server", err);
    }else{
        //console.log("Connected for Edit");

        var collection = db.collection('events');
        collection.updateOne({"_id":updId},{$set:query},function(err,result){
            if(err){
                console.log("Error adding message", err);
            }else if(result){
                //console.log("Update attempted", result.result);
                res.redirect('/admin');
            }else{
                console.log("Unknown error");
            }
        });
    }
  });
});

The GET route works fine when called directly, but seems to halt when called like this.

I'm pretty sure that there's nothing in the POST route that is causing this as the same thing happens when I strip out everything except the redirect itself.

router.post('/test',function(req,res){

    res.redirect('/admin');

});

Please help!

Drum
  • 505
  • 1
  • 5
  • 22
  • Please also include a sample of your input. What you do there with `.split()` and `.replace()` does not seem kosher to me. – Tomalak Sep 27 '16 at 14:40
  • The split and replace are to grab the required data for the update from the complete dogs breakfast of a multi-level JSON object which the DataTables editor sends as a request. It may not be elegant, but, pragmatically, the update is working. As I say above, even without any of that the redirect is not working (see edited question) – Drum Sep 27 '16 at 14:43
  • 1
    Don't *ever* use string methods on JSON. There is no room for the word "pragmatically" here. Parse. The. JSON. There is a free JSON parser in node, use it - it's painless and the proper thing to do. – Tomalak Sep 27 '16 at 14:46
  • Ok, I know that is right and I will do that, but I don't think it is related to the problem I am having here. – Drum Sep 27 '16 at 14:48
  • No, it isn't related, that's why I'm only commenting instead of answering. But it's a hideous bug waiting to happen in your current code, that's enough reason to make you aware of it. – Tomalak Sep 27 '16 at 14:55
  • 1
    I've been reading up on parsing json in express while waiting for answers here, so I will start doing things the correct way. – Drum Sep 27 '16 at 14:58
  • Just for the record, express has nothing to do with it. It's node.js core functionality. http://stackoverflow.com/questions/5726729/how-to-parse-json-using-node-js – Tomalak Sep 27 '16 at 15:02

0 Answers0