function generate(count) {
    var founded = false,
        _sym = 'abcdefghijklmnopqrstuvwxyz1234567890',
        str = '';
    while(!founded) {
        for(var i = 0; i < count; i++) {
            str += _sym[parseInt(Math.random() * (_sym.length))];
        }
        base.getID(string, function(err, res) {
            if(!res.length) {
                founded = true; // How to do it?
            }
        });
    }
    return str;
}

如何设置一个变量值与数据库查询回调?我该怎么做呢?


当前回答

从14.17.0开始,你现在可以使用内置的加密模块来生成uuid (UUIDv4 flavour):

const { randomUUID } = require('crypto'); // Added in: node v14.17.0

console.log(randomUUID());

// '89rct5ac2-8493-49b0-95d8-de843d90e6ca'

你可以探索更多 https://nodejs.org/api/crypto.html#crypto_crypto_randomuuid_options

注意:密码。randomUUID比uuid快三倍。而且不需要增加额外的依赖。

其他回答

你可以使用urid包npm安装urid

import urid from 'urid';

urid(); // qRpky22nKJ4vkbFZ

阅读完整的文档:https://www.npmjs.com/package/urid

// Set the size
urid(8); //ZDJLC0Zq

// Use the character set
urid('num'); // 4629118294212196
urid('alpha'); // ebukmhyiagonmmbm
urid('alphanum'); // nh9glmi1ra83979b

// Use size with character set
urid(12, 'alpha'); // wwfkvpkevhbg

// use custom character set
urid(6, '0123456789ABCDEF'); // EC58F3
urid('0123456789ABCDEF'); // 6C11044E128FB44B

// some more samples
urid()               // t8BUFCUipSEU4Ink
urid(24)             // lHlr1pIzAUAOyn1soU8atLzJ
urid(8, 'num')       // 12509986
urid(8, 'alpha')     // ysapjylo
urid(8, 'alphanum')  // jxecf9ad

// example of all character sets
urid('num')          // 5722278852141945
urid('alpha')        // fzhjrnrkyxralgpl
urid('alphanum')     // l5o4kfnrhr2cj39w
urid('Alpha')        // iLFVgxzzUFqxzZmr
urid('ALPHA')        // ALGFUIJMZJILJCCI
urid('ALPHANUM')     // 8KZYKY6RJWZ89OWH
urid('hex')          // 330f726055e92c51
urid('HEX')          // B3679A52C69723B1

// custom character set
urid('ABCD-')        // ACA-B-DBADCD-DCA

我想用这个

class GUID { Generate() { const hex = "0123456789ABCDEF"; const model = "xxxxxxxx-xxxx-4xxx-xxxx-xxxxxxxxxxxx"; var str = ""; for (var i = 0; i < model.length; i++) { var rnd = Math.floor(Math.random() * hex.length); str += model[i] == "x" ? hex[rnd] : model[i] ; } return str.toLowerCase(); } } console.log(new GUID().Generate()); console.log(new GUID().Generate()); console.log(new GUID().Generate()); console.log(new GUID().Generate()); console.log(new GUID().Generate());

对我来说,获取唯一id的最简单方法是时间,我在这个例子中使用哈希

const hash = require('object-hash');
const user = { ..., iat: Date.now() }
const user_id = hash(user)

console.log(user_id) // ex. 49686bab1a2276e0b1bd61ccc86f8156

这就是我在任何方面获得唯一标识符的方法。因为在现实世界中,时间是不会重复的。

安装uuid

npm install --save uuid

Uuid被更新,旧的导入

const uuid = require('uuid/v4');

不工作,我们现在应该使用这个导入

const {v4: uuid} = require('uuid');

把它作为一个函数来使用

const  createdPlace = {
    id: uuid(),
    title,
    description,
    location: coordinates,
    address,
    creator
  };

从14.17.0开始,你现在可以使用内置的加密模块来生成uuid (UUIDv4 flavour):

const { randomUUID } = require('crypto'); // Added in: node v14.17.0

console.log(randomUUID());

// '89rct5ac2-8493-49b0-95d8-de843d90e6ca'

你可以探索更多 https://nodejs.org/api/crypto.html#crypto_crypto_randomuuid_options

注意:密码。randomUUID比uuid快三倍。而且不需要增加额外的依赖。