admin管理员组文章数量:1406160
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?
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?
Share Improve this question asked Oct 23, 2015 at 10:42 TomasTomas 1,1412 gold badges12 silver badges25 bronze badges 1- Its quite weird as I tried it in empty document and it generates unique Id every time function gets called.. – Tomas Commented Oct 23, 2015 at 10:51
3 Answers
Reset to default 6Move v4()
call in to the function
function createFile(){
var uuid = nodeUuid.v4();
var filename = 'reports-'+uuid+'.txt';
}
Currently you're only generating a single UUID when the script is first loaded. To generate a new one each time you call createFile()
, simply move the generation inside your createFile()
function:
var nodeUuid = require('node-uuid');
function createFile(){
var uuid = nodeUuid.v4();
var filename = 'reports-'+uuid+'.txt';
}
...
createFile();
I might be late to the party, but you can do this without a library:
The script below is the minified version of node-uuid
v4 only library
import { default as crypto } from 'crypto'
class _Uuid {
static v4(offset = 0){
let byteToHex = [], rnds = crypto.randomBytes(16)
rnds[6] = (rnds[6] & 0x0f) | 0x40 // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
rnds[8] = (rnds[8] & 0x3f) | 0x80 // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
for (var i = 0; i < 256; ++i) { byteToHex[i] = (i + 0x100).toString(16).substr(1) }
return ([byteToHex[rnds[offset++]], byteToHex[rnds[offset++]], byteToHex[rnds[offset++]], byteToHex[rnds[offset++]], '-', byteToHex[rnds[offset++]], byteToHex[rnds[offset++]], '-', byteToHex[rnds[offset++]], byteToHex[rnds[offset++]], '-', byteToHex[rnds[offset++]], byteToHex[rnds[offset++]], '-', byteToHex[rnds[offset++]], byteToHex[rnds[offset++]], byteToHex[rnds[offset++]], byteToHex[rnds[offset++]], byteToHex[rnds[offset++]], byteToHex[rnds[offset++]]]).join('')
}
}
const v4 = _Uuid.v4
export { v4 as default }
Usage
import { default as uuid } from '_Uuid'
uuid() // 3ceb4f8f-cef9-400f-a394-80da0334273b
This is an ES6 script that uses the bundled crypto library in node and facilitates simple, fast generation of RFC4122 pliant v4 UUIDS. Source Code
本文标签: javascriptHow to generate unique UUID every time function is called in nodeJsStack Overflow
版权声明:本文标题:javascript - How to generate unique UUID every time function is called in nodeJs? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744382343a2603566.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论