最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using AngularJS's $resource to query a database - Stack Overflow

programmeradmin2浏览0评论

I've created an API that works via a url, built on node.js, express.js, mongoDB and angular.js.

My API is called like this:

app.get('/api/posts/:query', api.postsQuery);

So if I type localhost:3000/api/posts/<whateverquery>, mongoDB spits out all the pertinent JSON to my browser, so this is working just fine.

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:

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
  }
}

My issue is that when I run $scope.doSearch, I see the query in Chrome's resources panel like this: so the correct data is indeed being loaded, but it's not attaching itself to $scope.posts.

I have the feeling this might be because I need a callback function; I tried using callback: JSON_CALLBACK but that messes up my query/API (because it adds a ?callback=...to the end of the $resource call and this breaks the query).

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 app.get call, after :query to allow any wildcards afterward>

I've created an API that works via a url, built on node.js, express.js, mongoDB and angular.js.

My API is called like this:

app.get('/api/posts/:query', api.postsQuery);

So if I type localhost:3000/api/posts/<whateverquery>, mongoDB spits out all the pertinent JSON to my browser, so this is working just fine.

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:

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
  }
}

My issue is that when I run $scope.doSearch, I see the query in Chrome's resources panel like this: so the correct data is indeed being loaded, but it's not attaching itself to $scope.posts.

I have the feeling this might be because I need a callback function; I tried using callback: JSON_CALLBACK but that messes up my query/API (because it adds a ?callback=...to the end of the $resource call and this breaks the query).

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 app.get call, after :query to allow any wildcards afterward>

Share Improve this question asked Jul 9, 2013 at 16:04 JVGJVG 21.2k48 gold badges141 silver badges216 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

Add the callback to your $resource call:

$scope.doSearch = function(){
    $scope.itemSearch.get({query:$scope.searchTerm}, function(data){
        $scope.posts = data.posts;
        console.log($scope.posts[1]);
    });
};

Notice in the docs that they utilize the callback:

var User = $resource('/user/:userId', {userId:'@id'});
User.get({userId:123}, function(u, getResponseHeaders){
    u.abc = true;
    u.$save(function(u, putResponseHeaders) {
    //u => saved user object
    //putResponseHeaders => $http header getter
    });
});

Based on a looksie through the docs (and knowing AngularJS does everything asynchronously normally) it looks like the code below should work

$scope.doSearch = function(){
    $scope.posts = $scope.itemSearch.get({query:$scope.searchTerm}, function(){
        console.log($scope.posts[1]) // hopefully is something
    });
  }

http://docs.angularjs/api/ngResource.$resource

Since things are done asynchronously (to avoid blocking/waiting/pausing) there's usually either a callback or a promise for anything that is likely to take time (network calls).

The "Angular" way to solve this problem is by using the $q library for promises.

function indexCtrl($scope, $resource, $q) {
  var defer = $q.defer();

  var resource = $resource('http://localhost\\:3000/api/posts/:query', {query:''}, {get:{method:'JSONP'}});

  resource.get({query:'mySearchQuery'}, defer.resolve);

  defer.promise.then(function(data) {
    $scope.posts = data;
  });
}
发布评论

评论列表(0)

  1. 暂无评论