quickadd icon indicating copy to clipboard operation
quickadd copied to clipboard

fileCache.frontmatter?.tags?.split is not a function [BUG]

Open mtayyabviz opened this issue 3 years ago • 2 comments

Whenever I run this script, https://github.com/chhoumann/quickadd/blob/master/docs/Examples/Macro_MoveNotesWithATagToAFolder.md, after choosing the tag I keep getting fileCache.frontmatter?.tags?.split is not a function error.

Expected behavior It should move file notes based on hashtag

Desktop (please complete the following information):

  • Almalinux

mtayyabviz avatar Mar 29 '22 08:03 mtayyabviz

Hi @chhoumann , I'm having the same problem and would appreciate any hints on how to solve it!

jharst avatar Nov 18 '23 09:11 jharst

In case it's useful to anyone, this is a simplified version of the script (only checks for tags, not for Tags etc.) that works well for me. I simply had to add an typeof check.

module.exports = async function moveFilesWithTag(params) {
    const {
        app,
        quickAddApi: { suggester, yesNoPrompt },
    } = params;
    const allTags = Object.keys(app.metadataCache.getTags());
    const tag = await suggester(allTags, allTags);
    if (!tag) return;
    const shouldMoveNested = await yesNoPrompt(
        "Should I move nested tags, too?",
        `If you say no, I'll only move tags that are strictly equal to what you've chosen. If you say yes, I'll move tags that are nested under ${tag}.`
    );

    const cache = app.metadataCache.getCachedFiles();
    let filesToMove = [];

    cache.forEach((key) => {
        if (key.contains("template")) return;
        const fileCache = app.metadataCache.getCache(key);
        let hasFrontmatterCacheTag, hasTag;

        if (!shouldMoveNested) {
           hasFrontmatterCacheTag = typeof 
            fileCache.frontmatter?.tags === 'string'
                ? fileCache.frontmatter.tags.split(" ")
                .some((t) => t === tag.replace("#", ""))
                : fileCache.frontmatter?.tags?.some((t) => t === tag.replace("#", ""));
             hasTag = fileCache?.tags?.some((t) => t.tag.contains(tag));
        } else {
            hasFrontmatterCacheTag = typeof
             fileCache.frontmatter?.tags === 'string'
                ? fileCache.frontmatter.tags.split(" ")
                .some((t) => t.contains(tag.replace("#", "")))
                : fileCache.frontmatter?.tags?.some((t) => t.contains(tag.replace("#", "")));
            hasTag = fileCache?.tags?.some((t) => t.tag.contains(tag));
        }

        if (hasFrontmatterCacheTag || hasTag) filesToMove.push(key);
    });

    const folders = app.vault
        .getAllLoadedFiles()
        .filter((f) => f.children)
        .map((f) => f.path);
    const targetFolder = await suggester(folders, folders);
    if (!targetFolder) return;

    for (const file of filesToMove) {
        const tfile = app.vault.getAbstractFileByPath(file);
        await app.fileManager.renameFile(
            tfile,
            `${targetFolder}/${tfile.name}`
        );
    }
};

jharst avatar Nov 18 '23 13:11 jharst