簡體   English   中英

如何在貓鼬中結合兩個模型屬性生成唯一的密鑰哈希

[英]how to generate unique key hash in combination two model attribute in mongoose

我試圖在兩個模型屬性的組合中生成hash

我有這樣的架構

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

我想創建一個唯一的id ,它是empid and date組合的hash 。如果生成相同的hash ,則gives我錯誤。 我們可以結合empid and date生成unique嗎?

如果傳遞empid and date將會生成相同的哈希值? 那給我錯誤

這是我的代碼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);
  }

我以前使用過uuid庫。 Node還具有內置的md5哈希庫

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM