ga-wdi-exercises / pbj-project3

[project]
0 stars 2 forks source link

Syntax for processing join table queries #64

Closed tomBeach closed 9 years ago

tomBeach commented 9 years ago

Here is an example of why this is so frustrating; One of the few routes I have been able to get working is the one below (uncommented version), yet even when the route is successful it does not display console.log statements. (I even tried commenting out everything except the console.logs and still saw nothing!) I know the route itself works because when commenting the whole thing it fails (duh...).

More importantly, I can't figure out how to deal with the second selection criterion in the route ("group"). I am looking for stocks that are matched to the owner (user) via the ownership join table. Each record in that table also has a "group" column (e.g. "portfolio", "watchList", etc.), a string that should be searchable. When I add the group parameter to the route it fails. I can't tell if it's being passed in since nothing will console.log from this route. Or should group be passed in the params object? If so, how does it get into params from the ajax call (below route code). This process would be so much more understandable if these parameters could be passed as basic arguments as they are in so many other programming situations...

Beyond that, I do not understand where user.getStocks comes from. I don't see it defined anywhere in the code. I assume it's a "built-in" function, but I can't find a reference anywhere that explains the syntax of built-in functions. (Can it take arguments? Would it be "deleteStocks" or "updateStocks" for delete or patch requests?) Since we are actually dealing with the join table (which never appears in this code but is the basis for everything going on here) how does it know to look in the group column of the join table if the join table is never mentioned?

So many questions...

successful route

router.get("/users/:id/ownership/:group", function(req, res){
    console.log("GET: /users/:id/ownership");
    console.log("req.params: " + req.params);
    User.findById(req.params.id).then(function(user){
        if(!user) return error(res, "not found");
            user.getStocks().then(function(stocks){
            // user.getStocks({where: {user_id: req.params.user_id, group: req.params.group}}).then(function(stocks){
            // stocks.getStocks({where: {user_id: req.params.user_id, group: req.params.group}}).then(function(stocks){
            // ownership.getStocks({where: {user_id: req.params.user_id, group: req.params.group}}).then(function(stocks){
            res.send(stocks);
        });
    });
});

ajax call

        var url = "http://localhost:3000/users/2/ownership/" + this.groupName;  // NOTE groupName => group
        $.ajax({
            url: url,
            type: "get",
            dataType: "json"
        }).done(function(jsonData){
            console.log("  ajax request success!");
            console.dir(jsonData)
            extractDatabaseTickers(jsonData);
        }).fail(function(){
            console.log("  ajax request fails!");
            self.handleError;
        }).always(function(){
            console.log("  ajax request always");
            self.handleAlways;
        });
RobertAKARobin commented 9 years ago

Closing this to continue in the other thread.

RobertAKARobin commented 9 years ago

This might be helpful: http://stackoverflow.com/questions/25880539/join-across-multiple-junction-tables-with-sequelize

It looks like it includes the related data in the output of the findAll.