title: Using AngularJS's $resource to query a database
body: <p>I've created an API that works via a url, built on <code>node.js</code>, <code>express.js</code>, <code>mongoDB</code> and <code>angular.js</code>.</p>
  
  <p>My API is called like this:</p>
  
  <p><code>app.get('/api/posts/:query', api.postsQuery);</code></p>
  
  <p>So if I type <code>localhost:3000/api/posts/&lt;whateverquery&gt;</code>, mongoDB spits out all the pertinent JSON to my browser, so this is working just fine.</p>
  
  <p>However, I'm trying to link this up to my Angular front-end, and it's causing issues. I want the user to be able to search into a form and have my database return the correct records. Here's my controller:</p>
  
  <pre><code>function indexCtrl($scope, $http, $resource) {
    $scope.itemSearch = $resource('http://localhost\\:3000/api/posts/:query', 
      {query:''}, {get:{method:'JSONP'}});
  
    $scope.doSearch = function(){
      $scope.posts = $scope.itemSearch.get({query:$scope.searchTerm});
      console.log($scope.posts[1]) // returns undefined
    }
  }
  </code></pre>
  
  <p>My issue is that when I run <code>$scope.doSearch</code>, I see the query in Chrome's resources panel like this:<img src="https://i.stack.imgur.com/pNMTQ.png" alt="enter image description here"> so the correct data is indeed being loaded, but it's not attaching itself to <code>$scope.posts</code>.</p>
  
  <p>I have the feeling this might be because I need a callback function; I tried using <code>callback: JSON_CALLBACK</code> but that messes up my query/API (because it adds a <code>?callback=...</code>to the end of the <code>$resource</code> call and this breaks the query).</p>
  
  <p>Any ideas on what I can do here to get it working? Is the problem a lack of a callback? Perhaps I can add some regex to the <code>app.get</code> call, after <code>:query</code> to allow any wildcards afterward></p>