Accepted answer

Well, normally you'd let the server generate that id, then send it back down as part of the response for the axios call, but if you really want to generate an ID on a client, I suggest using uuid and one of it's various UUID generators:

example from the docs:

const uuidv4 = require('uuid/v4');
uuidv4(); // -> '110ec58a-a0f2-4ac4-8393-c866d813b8d1' 

You've added more code, and I think you're better served using uuidv4() on your server; something like:

function add (token, post) {
  return new Promise((res) => {
    let posts = getData(token)
    let id = uuidv4();

    posts[id] = {
      id: id,
      timestamp: post.timestamp,
      title: post.title,
      body: post.body,
      category: post.category,
      voteScore: 1,
      deleted: false,
      commentCount: 0


This way, the id is generated on the server. The client no longer needs depend on uuid, and you're not "trusting" the client to generate a valid id. The id would render back to the client as part of the response, and you could then use it from the client when doing edits, etc.

Related Query

More Query from same tag