Search code examples
javascriptnode.jsuuid

How to generate unique UUID every time function is called in nodeJs?


So I'm working on this project where I export some data files with sensitive information and I want to generate uuid for each of them. I'm using node uuid module but every time I run my function the UUID is actually the same and old file gets overwritten with a new file as its UUID is the same. Here's snipped of my code:

var nodeUuid = require('node-uuid');
var uuid = nodeUuid.v4();

function createFile(){
    var filename = 'reports-'+uuid+'.txt';
}
...
createFile();

So every time I call function createFile() I get the same UUID and my files are getting over-written, any idea how I could actually generate unique id for every new file?


Solution

  • Move v4() call in to the function

    function createFile(){
        var uuid = nodeUuid.v4();
        var filename = 'reports-'+uuid+'.txt';
    }