Why does `getAllTags()` return an empty array?

Hi, I am starting to learn javascript and the plugin API.

What is the difference between app.metadataCache.getTags() and getAllTags() ?

The getAllTags() method’s signature is given in the API line 1338 :

export function getAllTags(cache: CachedMetadata): string[] | null;

The getTags() method is not documented as far as I can see, but it does return the tags, whereas the getAllTags() method does not give me anything.

For example this code:

const cache = this.app.metadataCache;
const tags1 = getAllTags(cache);
console.log(`tags1.length = ${tags1.length}`);
const tags2 = cache.getTags();
console.log(`tags2.length = ${Object.keys(tags2).length}`);

gives output:

tags1.length = 0
tags2.length = 163

You’re not passing the correct property.
You need to get the cache of a file. What you’re passing right now is the metadata object on the API.

1 Like

Thanks! This works :smile: Any idea why app.metadataCache.getTags() is not documented?

I don’t know why it’s not a public API.

Anything that is not mentioned in obsidian.d.ts is considered to belong to private API (= methods and properties that Obsidian uses itself). Officially it means that plugin developers should not use it, as it might change without prior notification when Obsidian is updated.

In practise, there are plugins that do access private API. After all, it’s just about comparing risks and benefits: is it bad if the method belonging to a private API changes and breaks your plugin (or your plugin breaks Obsidian due to the change)? Is the feature you try to develop really worth the risk of breakage? Or is it about a rarely used side feature that does not hurt much if it breaks some day?

These are just my opinions.

2 Likes

Hi, guys. Can you tell what do I have to import to have access to the getAllTags() and getFileCache() functions? Thanks!

Here is the most important line for .getFileCache(): obsidian-shellcommands/VariableHelpers.ts at ac51071a5891cfae84eaf22f1566c1cc018c9543 · Taitava/obsidian-shellcommands · GitHub

The next line shows how to use the cache with getAllTags(). You don’t need to use the uniqueArray() function.

1 Like

Thanks!

1 Like