0

I am trying to send a request parameter through to an 'exports' method for a mongodb find in an express.js, backbone.js application. I am having a difficult time getting the parameters to pass through to mongodb and with '#'.

The breakage is the passing of parameters into the exported mongodb function.

Here is the flow of data:

First the request is successfully routed to the 'upcoming' function:

    "upcoming/uni/:uni" : "upcoming",

It flows on to the 'upcoming' function without a problem.

    upcoming: function(uni) {
    console.log("uni: "+uni);
    pag.reset();
    console.log("Hit upcoming list target");
    setCollectionType('upcoming');
        var upcomingCourses = buildCollection();

    // ------------------------------------------------------------------------
    // here is the problem how do I pass the parameter value through the fetch?
    // Although it may also have to do with '#' please read on.
    // ------------------------------------------------------------------------
        upcomingCourses.fetch({success: function(){
            $("#content").html(new ListView({model: upcomingCourses, page: 1}).el);
         }});
    this.headerView.selectMenuItem('home-menu');
},

The routing for the mongo methods is:

app.get('/upcoming/uni/:uni', mongomod.findUpcoming);

So the following method is exported from the mongodb js file and is executed reliable. However the req.params are not passed through. Interspersed in the code I have described its' runtime behaviour:

exports.findUpcoming = function(req, res) {
    console.log("university", req.params.uni); // This consistently is unpopulated
    var uni = req.params.uni;
    console.log("Size: "+req.params.length); // This will always be 0
    for (var i=0; i < req.params.length; i++) {
        console.log("Parameters: "+req.params[i]);
    }

    db.collection('upcoming', function(err, collection) {

    if (typeof uni === 'undefined') {
        console.log("The value is undefined");
        uni = "Princeton University"; // here we add a string to test it it will work.
    }

    collection.find({university:uni}).toArray(function(err, items) {
        if (err) {
            console.log("Error: "+err);
        } else {
            console.log("No Error");
            console.log("Count: "+items.length);
            console.log(items[0]['university']);
            res.send(items);
        }
     });
  });
};

On additional and important note:

The url, in a working, runtime environment would be:

http://localhost:3000/#upcoming/uni/Exploratorium

This one fails, but the following URL will work in passing the params through these functions however it returns the JSON to the screen rather then the rendered version:

http://localhost:3000/upcoming/uni/Exploratorium

The problem could be a miss understanding of # and templates. Please, if you see the error enlightenment would be greatly appreciated.

2 Answers2

1

Nothing after the # gets passed to the server. See How to get hash in a server side language? or https://stackoverflow.com/a/318581/711902.

Community
  • 1
  • 1
Trevor Dixon
  • 23,216
  • 12
  • 72
  • 109
  • The problem is that the URL parameters are not being passed internally. Your answer does clarify why there is success with one of the URL above, without a hash, as it is going directly to the server (Thank-you) and puts a better point on the question. – user988662 Oct 21 '13 at 22:42
0

I found a solution to the problem of passing the parameters from the client side to the server side. By changing the url of the collection the parameters will be passed to the server side:

upcomingCourses.url = "/upcoming/uni/"+uni; // <-- here's the ticket where uni is param
    upcomingCourses.fetch({success: function(){
        $("#content").html(new ListView({model: upcomingCourses, page: 1}).el);
 }});

This can be made more elegant but it is a way to pass the parameters on to the server.

Thanks