How to find a tag using the plugin-api

I have the following import statement in my code (javascript side):

import Tag from "discourse/models/tag";

However, I can’t use it to retrieve a tag object by it’s name. My goal is to obtain the tag description, but I only have the name. I was expecting something similar to this:

const tagObj = Tag.find("tag_name");
const tagDesc = tagObj.description;

Does the Tag model have a similar function?

1 Like

You can fetch tag data from the /tags.json endpoint.

In this example I’m getting an email tag’s description:

import { ajax } from "discourse/lib/ajax";
  ajax(`/tags.json`).then(({ tags }) => {
    const emailTag = tags.find((tag) => tag.name === "email");
    const emailTagDesc = emailTag.description;
  });
3 Likes

Thanks! I guess I found a more efficient way to accomplish this:

this.store.find("tag-info", "email").then(data => { 
 const tag_desc = data.description
});

However, I don’t know if this.store.find is faster than sending an ajax query to all tags and filtering. Maybe it’s doing the same thing.

2 Likes