0

So I've got a resource set up like so.

  .factory('ChannelListing', function($resource) {
    return $resource('/api/v1/channel_listings.json?channel_id=:channel_id',
    {channel_id: '@channel_id'}, 
    {'query':  {method:'GET', isArray:false}});
  })

Along with a conroller set up like so...

  .controller('ChannelListingCtrl', function($scope, ChannelListing) {
    ChannelListing.query(function(data){
      $scope.channellistings = data.results;
    });
  })

The issue is that I want the @channel_id to come via a user object that I'm pulling in from another resource set up like so...

  .factory('userFactory', function($resource) {
    return $resource('/users/me.json');
  })

This $resource returns all the user data from the current logged in users and contains the channel_id field. How can I get this field ID and pass it to the other resource?

Mr. BigglesWorth
  • 1,530
  • 3
  • 18
  • 33

1 Answers1

0

I believe you can do something like the following:

userFactory.query(function (user) {
    ChannelListing.query({channel_id: user.chanel_id}, function(data){
       $scope.channellistings = data.results;
    });
}));
Bahman
  • 68
  • 1
  • 3
  • So unless I'm just missing a simple braket or something this is returning the error "SyntaxError: Unexpected token i at Object.parse (native) at fromJson (http://127.0.0.1:9000/bower_components/angular/angular.js:1072:14)" – Mr. BigglesWorth Mar 24 '14 at 18:40
  • I forgot to add the full code sorry. `.controller('ChannelListingCtrl', function($scope, ChannelListing, userFactory) { userFactory.get(function (channel_id) { ChannelListing.query({channel_id: channel_id.chanel_id}, function(data){ $scope.channellistings = data.results; })}); });` – Mr. BigglesWorth Mar 24 '14 at 18:41
  • Syntactically it looks alright. If you are using chrome, you can easily put a break point in your code and check if you are getting what you expect. (Open up inspector, go to sources and pick the js file and put a break point in there, then you can actually type the variables in the console.) – Bahman Mar 24 '14 at 18:49
  • It must be that my factory isn't set up correctly for the user. For some reason I can't get it to pass correctly. I see it going to fetch /scripts/json/me.json in network, but it dies right after that. – Mr. BigglesWorth Mar 24 '14 at 19:14
  • Sorry new to the front end of things and as such new to using breakpoints. So if I set the breakpoint on this controller I get a pause when I refresh the page. This gives me no errors in console, but I'm not sure how to go about debugging to see where the remaining issue lies. Again, I'm sorry for being such a noobie, but I'm coming off a PHP background – Mr. BigglesWorth Mar 24 '14 at 19:39
  • Don't worry about it, to be honest debugging angular is a bit harder to debug compared to some of the other frameworks because of how it is built. Chrome comes with a very power full debugging dev tool, have a look at here: https://developers.google.com/chrome-developer-tools/docs/javascript-debugging. This could be a bit too detailed, just look at it quickly to see how it works. – Bahman Mar 24 '14 at 20:15
  • By the way, are you sure channel_listings.json does not return an array? The error you get is complaining about the parsing, so perhaps if you change isArray:false to isArray:ture, it might fix it. – Bahman Mar 24 '14 at 20:19
  • Looks like as it gets into the funtion it says arguments: [Exception: TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them] – Mr. BigglesWorth Mar 24 '14 at 20:25
  • Right, this is now talking about the 'strict mode' in js. Javascript is dynamically typed and you generally can have variables without instantiating it. But when you have 'use strict'; at the top of your file, it prevents you from writing 'bad' code. So either check if you have any variables which haven't been instantiated with the keyword var, or remove the 'use strict'; from the top of your file. (I'd say remove the 'use strict'; for now to see if you get the error or not) – Bahman Mar 24 '14 at 20:32
  • I've tested this without using the call to the user resource and it all seems fine. In your example when you're passing user to the function at `userFactory.query(function (user)` what is user representing? I assumed I would passing the field into there which is what I have in my follow up example. The user resource returns an object with: `{ id: 99384, name: "Jon Doe", buddy: "assets/1200471/1200471-buddy.jpg?1391891625", url: "/profiles/name", timezone: null, cube_id: 233007, channel_id: 2165811, pending_admin: false, denied_admin: false,}` so I'm looking to only pull that field – Mr. BigglesWorth Mar 24 '14 at 22:08
  • I think this has something to do with how I'm parsing the user json file. If I set it up like this: `.controller('ChannelListingCtrl', function($scope, $resource, ChannelListing, localUserFactory) { var User = $resource('scripts/json/me.json'); User.get(function(data){ $scope.channel_id = data; }); ChannelListing.query({channel_id: $scope.channel_id}, function(data){ $scope.channellistings = data.results; }); })` I get nothing passed to the channel ID either, however I still have the ChannelListing.query fire off just fine. – Mr. BigglesWorth Mar 24 '14 at 23:54
  • Things are getting stranger... So I decided to take out the other factory completely and just commented everything out but the user endpoint. Now I still get the same error with this.. `.controller('ChannelListingCtrl', function($scope, $resource, ChannelListing, localUserFactory) { var User = $resource('/scripts/json/me.json'); User.get(function(user){ $scope.channellistings = user.channel_id; });});` It almost seems like the output of the user JSON is somehow screwed up. Not sure why or how. – Mr. BigglesWorth Mar 25 '14 at 00:04
  • After all this time it turns out the json file I was using for local was corrupt.... I wasted a whole day on a bad file! lol. Anyway, you were a saint for sticking around with me for so long. Thanks again for all your help. Hope this helps others in the future. – Mr. BigglesWorth Mar 25 '14 at 00:45