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 use entries() with linked entry IDs? #14

Closed
raucao opened this issue Nov 18, 2014 · 11 comments
Closed

How to use entries() with linked entry IDs? #14

raucao opened this issue Nov 18, 2014 · 11 comments

Comments

@raucao
Copy link

raucao commented Nov 18, 2014

How can I use client.entries() with linked entry fields?

See https://support.contentful.com/hc/en-us/articles/200513278-How-do-I-add-categories-to-my-content-?page=1#comment_202252446 for use case.

@seidtgeist
Copy link
Contributor

When you query and set the include parameter to 1 or higher the links in entries will be resolved automatically for you:

client.entries({
  // filters
  include: 1 // 1 up to 10 levels of resolved includes
}).then(function(entries) {
  // use entries, e.g., if the first entry had a link in fields.linkToOtherEntry:

  console.log(entries[0].fields.linkToOtherEntry['en-US'].fields);
  // -> { ... } fields of linked entry
});

@raucao
Copy link
Author

raucao commented Nov 18, 2014

Ah, nice. Thanks!

@raucao raucao closed this as completed Nov 18, 2014
@raucao
Copy link
Author

raucao commented Nov 19, 2014

Sorry, have to re-open. How do I actually filter the results by category in this example? Downloading the whole data set would not work for us in all cases, and it seems the API supports it in general, right?

@sdepold
Copy link
Contributor

sdepold commented Nov 19, 2014

Hi @skddc,

if I understand your use-case correctly you have two content types which are linked and you want to read all entries of content type A which has a reference to a content type B and where the values of content type B matches a certain constraint, right?

Please note that such queries are currently not possible. What you can do though, is to read the wanted entry of content type B you are interested in and afterwards read all the entries of content type A which have fields.contentTypeA={IdOfContentTypeB}.

Or in code (and with categories/entries):

client
  .entries{{ "content_type": "CategoryID", "fields.someField": "someValue" })
  .then(function (category) {
    return client
      .entries({ "content_type": "EntryId", "fields.Category": category.sys.id })
      .then(function (entries) {
        // work with entries and/or category
        // you could also: return Bluebird.join(category, entries)
      });
  });

@seidtgeist
Copy link
Contributor

Going for another interptetation: If you want to get all Entries of type A which are in a certain category if type B this should work:

client.entries({
  include: 1,
  'fields.linkToCategoryOfTypeB.sys.id': 'idOfCategory'
});

As @sdepold said, you can't query for properties within the category, but you can still look for entries that link to a certain category ID.

@raucao
Copy link
Author

raucao commented Nov 20, 2014

@ehd Thanks, that was exactly what I was looking for!

@seidtgeist
Copy link
Contributor

🎉

@asross
Copy link

asross commented Feb 2, 2016

@ehd do you know if that's changed yet? I'd like to be able to query by associated category name without doing the intermediate request for the categories.

@seidtgeist
Copy link
Contributor

Someone please correct me if I'm wrong (cc @sdepold).

@asross It works the same as far as I know. What I do is keep a map from category names to IDs in a config file in my code so I can query by name. If your list isn't static, I would fetch the list at start up time, cache it and use that. I hope you find this helpful.

@sdepold
Copy link
Contributor

sdepold commented Feb 3, 2016

cc @trodrigues :D

@seidtgeist
Copy link
Contributor

Whoops. Thanks 😄

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

4 participants