[SOLVED] NODE+Superagent: Voting app routing - data not transferred?

I am not sure what is going on but I think the routing is not working correctly when I try to update a poll with a new vote. I can’t seem to reach the last route.

var express = require('express');
var router = express.Router();
var PollsCtlr = require('../controllers/PollsController');
var controllers = require("../controllers");

router.get("/:resource", function(req, res, next){
    console.log('inside API js - controller route');
    var resourceFrom = req.params.resource;
    var controller = controllers[resourceFrom];
    
    if (controller == null) {
        res.json({ confirmation: 'fail',
                    message: 'User made invalid resource request'
        });
        return;
    }
    
    // call the correct controller specified by the http request
    controller.find(req.query, function(err, results){
        if (err){
                res.json({ confirmation: 'fail',
                    message: err
                });
                return;
            }
            res.json({ confirmation: 'success',
                    message: results
        });
    });

  
});

router.get("/:resource/:id", function(req, res, next){
    var resource = req.params.resource;

    var id = req.params.id;
    var controller = controllers[resource];
    
    if (controller == null) {
        res.json({ confirmation: 'fail',
                    message: 'User made invalid resource request'
        });
        return;
    }
    
    // call the correct controller specified by the http request
    controller.findById(id, function(err, result){
           if (err){
                res.json({ confirmation: 'fail',
                    message: 'Not found'
                });
                return;
            }
            
            res.json({ confirmation: 'success',
                    message: result
                });
            
    });
    
});

router.post("/:resource", function(req, res, next){
    var resource = req.params.resource;

    var controller = controllers[resource];
    
    if (controller == null) {
        res.json({ confirmation: 'fail',
                    message: 'Invalid resource request on POST to: ' + resource
        });
        return;
    }
    
    controller.create(req.body, function(err, result) {
         if (err){
            res.json({ confirmation: 'fail',
                message: err
            });
            return;
        }
        
        res.json({ confirmation: 'success',
                message: result
            });
        
    });

});

// never arrives here >>
router.post("/:resource/:id", function(req, res, next){
    console.log("in api.js controller route ...");
    var resource = req.params.resource;
    
    var id = req.params.id;
    var controller = controllers[resource]; // select a controller specified in the URL
    
    if (controller == null) {
        res.json({ confirmation: 'fail',
                    message: 'Invalid resource request on POST to: ' + resource
        });
        return;
    }
    
    controller.update(id, req.body, function(err, result) { // call update function of specified ctlr
         if (err){
            res.json({ confirmation: 'fail',
                message: err
            });
            return;
        }
        
        res.json({ confirmation: 'success',
                message: result
            });
        
    });

});

module.exports = router;

Hi @JohnnyBizzel,

Hard to debug this way but I think because you are jumping into a new route after confirming receiving the data, you should use a “next()”. Not sure about that though…

Not sure how your implementation is but you are going from /:resource where you are apparently:

  • confirming receiving the data
  • applies a create method where you respond confirming the results (res.json(…))

That res is like a return of the function, closing it.

If I am not wrong, this would close the router without further action.

Not sure about that though… I think you should also check your front end implementation and verify the behaviour you want the user to follow…

1 Like

I am using Superagent but when it calls superagent the data disappears.

superagent
            .put(url)
            .type('json')
            .set('Accept', 'application/json')
            .send(body)
            .end((err, response) => {
                if (err) { 
                	console.log(err);
				    callback(err, null);
				    return;}
				// here check for API failures
				const confirmation = response.body.confirmation;
				if (confirmation != 'success') {
				    // send a failure message
				    callback({message:response.body.message, null});
				    return;
				}
				callback(response, response.body);
            })

So after put happens, when the .send(data) is called, there is nothing in the data object.

Screenshot:

This is the current API manager call (I was experimenting with different routes)

  var newVotesObj = { response: selectedRadio, votes: totalVotes};
        
        Api.put('/api/polls/put/' + pollId, newVotesObj, (err, response) => {
             if (err) { 
                 alert("Error: " + err); 
                 return;
             }
        });

I changed your title a bit to see if that helps to find someone that can help?

Your put doesn’t seem to have an issue:

Hi @JohnnyBizzel

Your using router.post("/:resource/:id", ...) instead of router.put("/:resource/:id", ...). So when super agent does a PUT request, it’s never going to reach that route because the types don’t match.

Changing the router to .put makes no difference.

I just had another look, I’m think the URL may be off.

Your router is expecting .put('/:resource/:id'), however, the URL in your screenshot only passes'/put/586{...}'. So it’s expecting a second param on that route but your only passing one.

Ok, seems to be working now. Seems like the problem is in the mongoose update command. :confused: