0

I am trying to generate hash in a combination of two model attribute.

I have schema like this

const BlogPost = new Schema({
  id: { type: String, required: true, unique: true },
  empid: String,
  date: Date
}); 

I want to create a unique id which is hash of the combination empid and date.if same hash is generated it gives me error. can we generate unique in a combination of empid and date ?

Same hash will be generated if we pass empid and date? then it gives me error

here is my code https://codesandbox.io/s/lively-tree-hd0fo

try {
    var blog = new BlogPostModel({
      empid: "test123",
      date: "19-Jul-2019"
    });
    console.log("before save");
    let saveBlog = await blog.save(); //when fail its goes to catch
    console.log(saveBlog); //when success it print.
    console.log("saveBlog save");
  } catch (error) {
    console.log(error);
  }
user944513
  • 12,247
  • 49
  • 168
  • 318

1 Answers1

0

I've used the uuid library for this before. Node also has a built in md5 hash library

  • 1
    I'm not sure what you mean here sorry, but I was able to find this stack overflow answer for uuid: https://stackoverflow.com/a/23327039/12060701 – Bram Adams Sep 13 '19 at 20:43