Nodejs UUIDv4 producing constant id value when used in Mongoose

前端 未结 1 341
半阙折子戏
半阙折子戏 2021-01-22 16:42

Recently I noticed that my UUIDv4 generator is having a high rate of collision. I use it to generate UUIDs for my mongodb objects like this:

const mongoose = req         


        
相关标签:
1条回答
  • 2021-01-22 17:23

    The problem with your code is UUID will be generated one time, and it will use that same default value.

    You need to use function form of default like this to generate a different uuid every time:

    UUID: { type: String, required: true, default: () => uuid() }
    
    0 讨论(0)
提交回复
热议问题