2

I am building an API with Restivus in Meteor.

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

Update This is my new fresh code with the same result. Use a standard Meteor project. 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
      }
    });
  });
}
  • This might be a bug. What you're doing looks correct. Feel free to report it via GitHub Issue (https://github.com/kahmali/meteor-restivus/issues), but I'll look into it in the meantime. – kahmali Mar 05 '15 at 05:58
  • I just ran some tests and I'm able to access multiple query params exactly how you've shown it here. Is that really all you're doing? value1 should definitely be defined there. hmmmm.... – kahmali Mar 05 '15 at 06:16
  • Thank you for helping out. BTW, Restivus is really cool! I don't know whats happening here but that problem still occurs. Made a new project with only Restivus installed. Same thing. Plus: new errors: `I20150306-09:36:57.284(1)? key: 1234 I20150306-09:36:57.284(1)? value1: undefined W20150306-09:36:57.285(1)? (STDERR) TypeError: Cannot read property 'body' of undefined` – Eugen Pflüger Mar 06 '15 at 08:37
  • Could you post or send me your project? Its really strange. I always get the same error. – Eugen Pflüger Mar 06 '15 at 15:58
  • I tested it using Tinytest on the package. I just pushed it to the 'tinytest' branch: https://github.com/kahmali/meteor-restivus/tree/tinytest. Just pull that down and run the command `meteor test-packages ./` from the root directory. You'll see the test output in your browser at localhost:3000. The tests begin on line 93 and 107 in the api_tests.coffee file. It would help me a lot if you would open this as an issue on GitHub (the link is in my first comment).... – kahmali Mar 07 '15 at 06:57
  • ...StackOverflow is not really the best place for a discussion around debugging the package. I'll also receive emails when you respond so I can get back to you sooner. (case in point, this response was too long and needs to be broken up, and if I put anything like this in an answer the SO police will come get me).... Oh, and thanks for the compliment on Restivus! I really appreciate it! – kahmali Mar 07 '15 at 07:03
  • Understood. Thanks. Will file a GitHub issue. – Eugen Pflüger Mar 09 '15 at 08:19

1 Answers1

4

This is the solution to the problem. Taken from here: https://github.com/kahmali/meteor-restivus/issues/16

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.

  • Here's the link to that SO question where I found the answer: http://stackoverflow.com/questions/10978474/appending-multiple-querystring-variables-with-curl – kahmali Mar 11 '15 at 08:19