Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
let slug = state.data.title.trim().replaceAll(" ", "-").toLowerCase();

what i should get after adding multiple posts using the same title

title
title-1
title-2
title-3

what I get
title
title
title
title

I am using Axios and JSON server

data.json -

{
"posts": [
{
"id": 1,
"title": "title",
"slug": "slug"
},
{
"id": 2,
"title": "title",
"slug": "slug"
},
{
"id": 3,
"title": "title",
"slug": "slug"
}
]
}

What I have tried:

{
    "posts": [
        {
          "id": 1,
          "title": "title",
          "slug": "slug"
        },
        {
          "id": 2,
          "title": "title",
          "slug": "slug"
        },
        {
          "id": 3,
          "title": "title",
          "slug": "slug"
        }
      ]
    }
Posted
Comments
Richard Deeming 16-May-22 10:42am    
Why not just append the id to the slug?
MS The Tech Guy 16-May-22 12:45pm    
I already thinked of it but suppose if we add 18th post called how to center a div so the slug would be how-to-center-a-div-18 and this would not be great for user. so now I added this bottom to title input "Slug with this title already used. Change it slightly to remove this error" Is this a good idea?
Jo_vb.net 16-May-22 14:08pm    
You could write the id within brackets

how-to-center-a-div [18]
or
how-to-center-a-div [id=18]
MS The Tech Guy 16-May-22 23:41pm    
slug will be displayed in top bar so special characters wont run
Richard Deeming 17-May-22 3:26am    
Well either you need to identify the post from the slug, which means having the ID within the slug somewhere; or you already have the ID elsewhere in the URL, in which case why worry about "duplicate" slugs?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900