UX Guy
UX Guy

Reputation: 257

My $resource query is returning all the objects when I have parameters for just one _id

I'm trying to find just the store that matches my id parameter. In terminal everything looks fine:

GET /admin/company?_id=5470e913d20b3dab7c13218b 200 219.590 ms - -

And then here's my $resource

angular.module('mean.management').factory('Companies', ['$resource',
    function($resource) {
        return $resource('/admin/company/:companyId', {
            companyId: '@_id'
        }, {
            update: {
                method: 'PUT'
            },
            get: {method: 'GET',isArray: true}
        });
    }
]);

It's searching with the right _id but it returns a list of a bunch of companies instead of just the one company.

Here's the controller

    $scope.newStore = function () {
        Companies.get({
            _id: $scope.global.user.company // _id works fine
        }, function (company2) {

            console.log(company2); // this logs 40 different companies when it should be one matching the _id
        });

    };

If I replace Companies.get with Companies.query, it's the same problem. Doesn't change anything. I also changed get: {method: 'GET', isArray: false} instead of true, and that just returns an error in the browser console (because it's an array).

Update: I know the parameter is correct because if I go to localhost:3000/admin/company?_id=5470e913d20b3dab7c13218b and ctrl+f 5470e913d20b3dab7c13218b I can see the object with that _id in it.

Update2: I think I'm getting closer to a solution. If I comment out this, it doesn't return any articles

// routes
app.get('/admin/company', companies.all); // if I comment this, it won't return anything
app.get('/admin/company/:companyId', companies.oneCompany); // this doesn't seem to be doing anything

Update3 Here are some examples of server-side code

exports.oneCompany = function(req, res, next, id) {
  Company.load(id, function(err, article) {
    if (err) return next(err);
    if (!article) return next(new Error('Failed to load article ' + id));
    req.article = article;

        res.json(req.article);
    next();
  });
};

Here's another one I tried

exports.company1 = function (req, res, next, id) {
    Company
        .findOne({
            _id: id
        })
        .exec(function (err, user) {
            if (err) return next(err);
            if (!user) return next(new Error('Failed to load User ' + id));
            req.profile = user;
            res.json(user);
            next();
        });
};

Upvotes: 0

Views: 582

Answers (1)

Antti Latva-aho
Antti Latva-aho

Reputation: 111

I am fairly new to ngresource myself and figured similar things out last week my self. Your issue here is the resource definition where you generate the companyId into URL path but not as parameter when you define the resouce like this:

return $resource('/admin/company/:companyId', {
   companyId: '@_id'
},

What you would need is just simply this:

return $resource('/admin/company') 

And use this $resource as

app.get({_id:companies.oneCompany});

This generates resource URLs as /admin/company?_id=XYZ. This part is documented in https://docs.angularjs.org/api/ngResource/service/$resource as paramDefaults:

Each key value in the parameter object is first bound to url template if present and then any excess keys are appended to the url search query after the ?.

Given a template /path/:verb and parameter {verb:'greet', salutation:'Hello'} results in URL /path/greet?salutation=Hello.

Hope this helps.

Upvotes: 2

Related Questions