Skip to content
This repository has been archived by the owner on Oct 11, 2022. It is now read-only.

REST access not working for publications with arguments #157

Open
ptandler opened this issue Sep 7, 2020 · 3 comments
Open

REST access not working for publications with arguments #157

ptandler opened this issue Sep 7, 2020 · 3 comments

Comments

@ptandler
Copy link

ptandler commented Sep 7, 2020

Using the simple:rest package to access publications works very nicely!

However, I didn't manage yet to GET a publication that requires an argument.

  • When I call the GET URL without argument http://localhost:3000/publications/export/, I get an error message (as I check for the argument in my publication)
  • When I call get URL with attached argument: http://localhost:3000/publications/export/12345/ the main app gets loaded instead (well, with some broken links to images)
  • When I add an url option to the publication
  Meteor.publish("export", function export() {...}, {
    url: "export/:0",
  })

it doesn't help, I still get the main app.

What am I missing?

@nooitaf
Copy link
Contributor

nooitaf commented Oct 1, 2020

Did you try it without the trailing slash?
As in export/12345 instead of export/12345/

You also don't use the argument as it gets parsed to the export function.
So your publish should probably look more like this..

Meteor.publish("export", function export(argument_from_url) {...}, {
  url: "export/:0",
})

@ptandler
Copy link
Author

ptandler commented Oct 5, 2020

Thanks for your suggestion. I also tried without the trailing slash now.
In fact, when I call http://localhost:3000/publications/export/12345 it redirects to http://localhost:3000/publications/export/12345#/ with "#/" appended. I guess because I use the default routing mode?
However, calling http://localhost:3000/#/publications/export/12345 also doesn't work.

When I check http://localhost:3000/publications/api-routes, the export route is listed.

And you're right, in the example above I was missing the definition of the argument of the export function.

Any other ideas ...?

@nooitaf
Copy link
Contributor

nooitaf commented Oct 8, 2020

I think what is happening is that you use the /publications/... link which only gets populated if you dont add the { url: "item/:0" } in the publication. As soon as you add the url those don't work auto-magically anymore. You should just remove the /publication/ and be golden.


Did a little test and it works as expected for me.

$ meteor add simple:rest
// server code
import {Mongo} from 'meteor/mongo';
export const Items = new Mongo.Collection('items');

if (Meteor.isServer) {

  // create some items
  Meteor.startup(function() {
    if (!Items.find().count()) {
      Items.insert({ name: "test1" })
      Items.insert({ name: "test2" })
      Items.insert({ name: "test3" })
    }
  })

  Meteor.publish('items', function items(item_name) {
    if (item_name) {
      return Items.find({
        name: item_name
      });
    } else {
      return Items.find({});
    }
  }, {
    url: "item/:0"
  });

}

cURL'ing the items like this, with and without slash

$ curl localhost:3000/item/test1
{
  "items": [
    {
      "_id": "hkBBrBpn9obntiviG",
      "name": "test1"
    }
  ]
}
curl localhost:3000/item/test1/
{
  "items": [
    {
      "_id": "hkBBrBpn9obntiviG",
      "name": "test1"
    }
  ]
}

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

No branches or pull requests

2 participants