Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to read multiple queryParams #16

Closed
plugisto opened this issue Mar 9, 2015 · 8 comments
Closed

How to read multiple queryParams #16

plugisto opened this issue Mar 9, 2015 · 8 comments

Comments

@plugisto
Copy link

plugisto commented Mar 9, 2015

In a custom route I would like to have multiple values as queryParams like this (e.g. value1 and value2):

...domain/api/update?key=1234&value1=10
How do I get them in endpoint function?

When I try this I get undefined:

var query = this.queryParams.key // result: 1234
var value1 = this.queryParams.value1 // result: undefined

This is my code in a new standard Meteor project with only Restivus package added.
Meteor version 1.0.3.2

// Create collection
Posts = new Mongo.Collection("posts");

if (Meteor.isServer) {

  Meteor.startup(function () {
    // RESTIVUS
    // Global configuration
    Restivus.configure({
      useAuth: false,
      prettyJson: true
    });


    // Given the url: "/posts?key=1234&value1=10"
    Restivus.addRoute('posts', {
      get: function () {
        var key = this.queryParams.key;
        var value1 = this.queryParams.value1;
        console.log("key: " + key); // result: 1234
        console.log("value1: " + value1); // result: undefined
      }
    });
  });
}
@plugisto
Copy link
Author

plugisto commented Mar 9, 2015

As suggested on Stackoverflow (http://stackoverflow.com/questions/28859371/meteor-restivus-how-to-read-multiple-queryparams?noredirect=1#comment46083182_28859371) here is the test result.
I am not really sure what to do with the test
[A] I just run the test in the project that you did send, right?
[B] Or should I include the project into my project and run the test?

Here is the result from [A]:
bildschirmfoto 2015-03-09 um 12 24 03

@kahmali
Copy link
Owner

kahmali commented Mar 9, 2015

If you look at the last two tests there, you'll see that they are designed to test what you were asking about (an endpoint being able to access multiple query params. If you look at the tests for those you'll see that I'm testing exactly what you're doing above:

describe 'An endpoint', ->
  it 'should have access to multiple query params', (test, next) ->
    Restivus.addRoute 'mult-query-params',
      get: ->
        test.equal @queryParams.key1, '1234'
        test.equal @queryParams.key2, 'abcd'
        test.equal @queryParams.key3, 'a1b2'
        true

    HTTP.get 'http://localhost:3000/api/v1/mult-query-params?key1=1234&key2=abcd&key3=a1b2', (error, result) ->
      test.isTrue result
      next()

describe 'A collection endpoint', ->
  it 'should have access to multiple query params', (test, next) ->
    Restivus.addCollection new Mongo.Collection('TestQueryParams'),
      path: 'mult-query-params-2'
      endpoints:
        getAll:
          action: ->
            test.equal @queryParams.key1, '1234'
            test.equal @queryParams.key2, 'abcd'
            test.equal @queryParams.key3, 'a1b2'
            true

    HTTP.get 'http://localhost:3000/api/v1/mult-query-params-2?key1=1234&key2=abcd&key3=a1b2', (error, result) ->
      test.isTrue result
      next()

As you can see in both of those tests, I'm testing for the values of multiple query params to verify having access to them. I do this by making an HTTP request with all the query params in the url and then testing for those values in my GET endpoint. As you can see from running the tests, all tests pass, meaning I'm able to access all three of the query params.

@plugisto
Copy link
Author

plugisto commented Mar 9, 2015

I believe you :)
But what am I doing wrong? I just can't find anything.
Have you tested my code from first post above? Any idea what could be wrong?

@kahmali
Copy link
Owner

kahmali commented Mar 10, 2015

I just tested your code and it worked fine. The only thing I had to do is what I discussed in your other issue (#17), as far as making sure something was returned from the endpoint. Here is the code, but again, nothing is different except making sure something is returned. For this example, I return whatever values you pass as query params. If you would like to test it out, just make a GET request to http://testivus.meteor.com/api/posts?key=1234&value1=10. Here is the code being hosted there:

if (Meteor.isServer) {

  Meteor.startup(function () {
    // RESTIVUS
    // Global configuration
    Restivus.configure({
      useAuth: false,
      prettyJson: true
    });


    // Given the url: "/posts?key=1234&value1=10"
    Restivus.addRoute('posts', {
      get: function () {
        var key = this.queryParams.key;
        var value1 = this.queryParams.value1;
        console.log("key: " + key); // result: 1234
        console.log("value1: " + value1); // result: 10
        return {
          key: key,
          value1: value1
        }
      }
    });
  });
}

@plugisto
Copy link
Author

Thanks for your support! But it's still not working. I think I am going crazy.
I made a GET call to your link - it works there.

When I paste your code into a new project I get "undefined" again.
Last idea: here are two screenshot from my code and the terminal windows.
Maybe you can tell if I am doing something really wrong?

bildschirmfoto 2015-03-10 um 15 01 48
bildschirmfoto 2015-03-10 um 15 03 27

@kahmali
Copy link
Owner

kahmali commented Mar 10, 2015

I was this close to thinking I was losing my mind as well, but I figured it out! You're using curl to test, right? Well apparently (and don't feel bad for not knowing this, because neither did I), the & symbol means that the previous command will be run in the background, so the query params were just being truncated once the curl command reached the & for the second query param. All you have to do is wrap the URL in quotes, and voila! Try this command instead: curl "http://testivus.meteor.com/api/posts?key=1234&value1=10". That should work. So if you had just punched that URL into a browser or used a mored advanced REST client, you would have seen the extra query param defined. I got the answer from this StackOverflow question.

Let me know if this fixes your problem. Hopefully, this prevents you from losing any further sanity!

@plugisto
Copy link
Author

F***... it works!
Thanks a lot! Was not aware of that thing with & symbol.
Thank you for your help!
Close!

@kahmali
Copy link
Owner

kahmali commented Mar 10, 2015

Haha. Awesome! Glad we finally got to the bottom of this. Thanks for closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants