$resource - How do I pass query parameters with PUT method

24,493 views
Skip to first unread message

chris...@gmail.com

unread,
Jun 24, 2012, 1:31:14 AM6/24/12
to ang...@googlegroups.com
Hi,


var User = $resource(url, {userId:'@id'} , {add: {method: PUT, params:{location:@loc} }   }

User.add({id:123, loc:'glasgow'});

When I am using the PUT method the above data passed to the User add function is  being passed as JSON data rather than url query parameter. Is there a way to pass url query parameters with the PUT method using $resource?

Thanks,
Chris.

Dan Doyon

unread,
Jun 24, 2012, 2:33:48 AM6/24/12
to ang...@googlegroups.com
Chris,

Take another close look at the example on ngResource doc http://docs.angularjs.org/api/ngResource.$resource

  1. // our custom method is mapped as well.
  2. card.$charge({amount:9.99});
  3. // POST: /user/123/card/456?amount=9.99&charge=true {id:456, number:'1234', name:'J. Smith'}

PUT should have same behavior as POST in that the parameters you don't define with '@' (in this case 'amount' ) will be added as query params. If however, in your definition of the PUT resource you had {id: '@id', amount: '@amount' } then it would be passed along as JSON

--dan


--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/2ECI4qIoUgkJ.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/angular?hl=en.

chris...@gmail.com

unread,
Jun 24, 2012, 2:52:20 PM6/24/12
to ang...@googlegroups.com
I have read the docs. Doesn't work for PUT. Will try to make a fiddle....

Chris.
To unsubscribe from this group, send email to angular+unsubscribe@googlegroups.com.

Marcus

unread,
Aug 14, 2012, 2:31:50 PM8/14/12
to ang...@googlegroups.com, chris...@gmail.com
Hi

I've been reading through this thread today, as I've run into a complete roadblock with this same issue.

We want to use one .factory to run MongoLab databases -- looking to store many "collections" or tables of data, and just using a single .factory with the same concept of  "var User = $resource(url, {userId:'@id'} , {add: {method: PUT, params:{location:@loc} ..."

After hours of testing every possible alternative, I don't see any way to solve the PUT problem.

Should we call this a SOFTWARE BUG and ask someone to try to fix it ?

I've got everything else working, but this is a real show stopper and a big downer for our project.

Cheers,

Marcus


Take another close look at the example on ngResource doc http://docs.angularjs.org/api/ngResource.$resource

  1. // our custom method is mapped as well.
  2. card.$charge({amount:9.99});
  3. // POST: /user/123/card/456?amount=9.99&charge=true {id:456, number:'1234', name:'J. Smith'}

PUT should have same behavior as POST in that the parameters you don't define with '@' (in this case 'amount' ) will be added as query params. If however, in your definition of the PUT resource you had {id: '@id', amount: '@amount' } then it would be passed along as JSON
--dan
Hi,

Pawel Kozlowski

unread,
Aug 14, 2012, 2:37:25 PM8/14/12
to ang...@googlegroups.com
Marcus,

I've got PUT working with MongoLab without any issues... The only
thing you need to know is that you need to remove the it from the
object you send (and this is what I'm doing in my little wrapper
around $resource for MongoLab).

Is there any chance that you would share a code snippet (jsFiddle /
plunker)? Those things do work and you are probably bumping into some
syntax issues but it is extremely hard to help without seeing code
examples...

Cheers,
Pawel
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To post to this group, send email to ang...@googlegroups.com.
> To unsubscribe from this group, send email to
> angular+u...@googlegroups.com.
> Visit this group at http://groups.google.com/group/angular?hl=en.
>
>

Marcus

unread,
Aug 14, 2012, 6:46:49 PM8/14/12
to ang...@googlegroups.com
Hi Pawel,

I managed to create this Fiddle:  http://jsfiddle.net/mcHx5/10/

I was able to get the records with :    $scope.cities = MongoLab('recursos').query();

   (please note that I use "cities" instead of "projects" and use "recursos" as the name for this "collection")

I tried using your "wrapper", which works nicely for the .query() feature.  However, when I try to do a GET on an existing record, the Mongo server returns the record, but then my app crashes with:

Error: XML filter is applied to non-XML value function MongoCollection(collectionName) {var MongoLab =...

I tried to create new records, and also had too many problems.

Things get more complicated by this time, and so I've stopped here and looked for other ways to inject the "collection" without using your "wrapper". Also frustrated that it works in some cases, but not with PUT.

For both approaches I am just stuck and frustrated after spending several days on this.

Hope you can help me,

Thanks,

Marcus


Pawel Kozlowski

unread,
Aug 15, 2012, 7:12:24 AM8/15/12
to ang...@googlegroups.com
Marcus,

I'm sorry that you've lost few days and got frustrated, I would help
earlier if I would have a jsFiddle... This is very important to see
the code to be able to help effectively.

Anyway, I've started to change your jsFiddle and got this working:
http://jsfiddle.net/pkozlowski_opensource/7XyKh/7/
As you can see the code is pretty simple!

I don't have time to continue working on this at the moment but I hope
it will get you going. I will come back to this code later today and
will try to send a complete working version.

Hope this helps,
Pawel

Pawel Kozlowski

unread,
Aug 15, 2012, 9:49:14 AM8/15/12
to ang...@googlegroups.com
Hi again Marcus,

So, here is the jsFiddle with all the CRUD operations working:
http://jsfiddle.net/pkozlowski_opensource/7XyKh/12/
It correctly handles edits (PUT request) and delete (DELETE). I could
clean-up this fiddle more but this wouldn't bring anything with
respect to mongolab usage.

In the above fiddle I'm still using my mongolab wrapper
(https://github.com/pkozlowski-opensource/angularjs-mongolab) since it
removes so much pain of working with mongolab (messing with _id.$oid
etc.) and add some nice shortcut methods (getById()).

It is hard to say why you were running into so many problems but I
hope that now, with the live example, you will be able to move
forward.

Hope this helps,
Pawel

On Wed, Aug 15, 2012 at 12:46 AM, Marcus <bo...@befund.com> wrote:

Marcus

unread,
Aug 15, 2012, 11:23:29 AM8/15/12
to ang...@googlegroups.com
Dear Pawel,

THANK YOU SO MUCH !!!

I am amazed by how you can simplify everything into a very nice work of coding.

Now I see why FIDDLEs are so good to work with :-)

I've been designing systems and coding software since 1980's, from mainframes, client-server, web systems and what is now legacy to a younger generation.  I bet my son would understand AngularJS better because he starts with a clean sheet of paper.  But, I need to prove to myself that we can develop on a platform that will be around in 10 years from now.  My engineering teams are skeptical until they see me actually delivering some awesome and radical solutions (which I'm sure we'll do).

You were very generous with your time, and I will try to return the favor by publishing the "framework" that we're developing.  The idea is to have an architecture that we can give to several developers of different functions, and have one IT architect that can delegate small applets for our business (agroindustrial producer, exporter and trading company in South America).

Meanwhile, I took the liberty to add back the openNew() "+" function on tab 1

This is the modified fiddle:   http://jsfiddle.net/7XyKh/14/

Its not quite there, as when I click on the SAVE button in tab 2, I still need to jump back to tab 1 and I must also erase (inactivate ?) the fields left on screen (tab 2).

Its an excellent demo for using your "wrapper" --  perfect to create many "collections" (tables) in a MONGO database.

Thank you for your precious time !

Marcus

Christian Moise

unread,
Apr 25, 2016, 1:36:18 AM4/25/16
to AngularJS, chris...@gmail.com
Hey Chris,
Try User.add({userid:123, loc:'glasgow'}); instead and see if it works

Sander Elias

unread,
Apr 25, 2016, 1:53:13 AM4/25/16
to AngularJS, chris...@gmail.com
Hey Cristian,

I'm pretty sure he got it figured out by now...  Jeezz, that question is 4 years old ;)

Regards
Sander
Reply all
Reply to author
Forward
0 new messages