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

collection.getFilteredByTag is not a function when populating collection via API #1153

Closed
piyushpaliwal opened this issue May 6, 2020 · 3 comments

Comments

@piyushpaliwal
Copy link

Describe the bug

I am trying to implement the solution for previous and next links on my project (#529). My collections are getting populated via an async API call.

After the collection is populated, I am trying to call the function getFilteredByTag but it fails with the error getFilteredByTag is not a function. I may be doing something wrong but can you help identify what could be going wrong in this case?

Screenshots

image

Environment:

  • OS and Version: Ubuntu 18.04 (WSL)
  • Eleventy Version: 0.10.0

Additional context

Below is the collection method that I am utilizing to bring data from my ghost installation.

// Get Featured Posts
eleventyConfig.addCollection("featured", async function (collection) {
    collection = await api.posts
        .browse({
            include: "tags,authors",
            limit: "4",
            order: "published_at desc",
            filter: 'featured:true'
        })
        .catch(err => {
            console.error(err);
        });

    collection.forEach(post => {
        post.url = post.primary_tag.slug + stripDomain(post.url);
        post.primary_author.url = stripDomain(post.primary_author.url);
        post.tags.map(tag => tag.url = stripDomain(tag.url));
        post.authors.map(author => author.url = stripDomain(author.url));
        post.published_at = moment(post.published_at).format("Do MMM YYYY");
    });
    const coll = collection.getFilteredByTag("featured");

    for (let i = 0; i < coll.length; i++) {
        const prevPost = coll[i - 1];
        const nextPost = coll[i + 1];

        coll[i].data["prevPost"] = prevPost;
        coll[i].data["nextPost"] = nextPost;
    }
    return coll;
});
@piyushpaliwal
Copy link
Author

I have managed to fix this issue by simply adding nextPost and prevPost in the foreach loop like below:

// Get Featured Posts
    eleventyConfig.addCollection("featured", async function (collection) {
        collection = await api.posts
            .browse({
                include: "tags,authors",
                limit: "4",
                order: "published_at desc",
                filter: 'featured:true'
            })
            .catch(err => {
                console.error(err);
            });
        collection.forEach((post, index) => {
            post.url = post.primary_tag.slug + stripDomain(post.url);
            post.primary_author.url = stripDomain(post.primary_author.url);
            post.tags.map(tag => tag.url = stripDomain(tag.url));
            post.authors.map(author => author.url = stripDomain(author.url));
            post.published_at = moment(post.published_at).format("Do MMM YYYY");
            post.prevPost = collection[index - 1];
            post.nextPost = collection[index + 1];
        });
        return collection;
    });

However I would still like to know if collection methods will be available if it is getting populated via an API.

zachleat added a commit to 11ty/11ty-website that referenced this issue May 17, 2020
@zachleat
Copy link
Member

I think you’ve misunderstood the argument there. I’ve changed the docs to reflect this but it’s more accurate to read it like this:

eleventyConfig.addCollection("featured", async function (collectionApi) {

So what you’re doing there is effectively monkey patching the collections API, which is probably not what you want. Now, you can return any arbitrary object from your collection but we don’t modify those in any way.

https://www.11ty.dev/docs/collections/#return-values

All of that being said, I don’t think collections are the right mechanism here. JavaScript Data Files will probably work much better https://www.11ty.dev/docs/data-js/

@zachleat
Copy link
Member

This is an automated message to let you know that a helpful response was posted to your issue and for the health of the repository issue tracker the issue will be closed. This is to help alleviate issues hanging open waiting for a response from the original poster.

If the response works to solve your problem—great! But if you’re still having problems, do not let the issue’s closing deter you if you have additional questions! Post another comment and I will reopen the issue. Thanks!

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

No branches or pull requests

2 participants