Accidentally creating duplicate tags via AdminAPI

UPDATE AT THE BOTTOM.

Latest Ghost, Ghost Pro hosted.

I’m using the Admin API SDK to add some tags to posts. My code looks like this:

    let posts = await api.posts.browse({filter: `tag:${tagSlug}`, limit: 'all'});
    console.log('got posts #', posts?.length);
    for (let post of posts) {
        let tags = post.tags.map(tag => tag.name);
        if (tags.includes(extraTagName)) {
            console.log('already has tag', extraTagName);
        } else {
            console.log('adding tag', extraTagName);
            tags.push(extraTagName);
            post.tags = tags;
            await api.posts.edit(post);
            await pause(100);
        }
    }

Where tagSlug is being used to identify posts that have an existing tag, and extraTagName is the name of the existing tag to be added.

When I run this, new tags with extraTagName are getting created, instead of the existing tag being used.

The docs seem to say this is the right way, and the short form of tags should use names – see Ghost Admin API Documentation

So… are the docs wrong, or am I having a bad case of Friday afternoon?

UPDATE: It’s a Friday afternoon. I’ve got a whitespace problem with my input. ARGH.

1 Like