gpt4 book ai didi

Node.js 生成随机唯一 id 并通过 Do .. While 循环检查 mongoDB 是否存在

转载 作者:太空宇宙 更新时间:2023-11-04 03:19:44 25 4
gpt4 key购买 nike

我正在尝试编写 do...while 语句。在主体内部生成随机 ID,并在一段时间内检查数据库中是否存在。如果ID不存在,则退出while。我对回调有点困惑,如果有人可以帮助我,我将非常感激!

是否必须生成数字或字符串并不重要...

最佳答案

用于 do while 循环的函数,我使用 async/await 代替回调。

async function checkUniqueId() {

do {
id = 'sadfasdrfawerwer';//some generated random id;

} while (await checkInDb(id));


}

检查数据库中id的函数

async function checkInDb(id) {
try {
let idFound = await User.findOne({ id: id });
if (idFound) return true;
else return false;
}
catch (err) {
console.log(err);
return false;
}

}

关于Node.js 生成随机唯一 id 并通过 Do .. While 循环检查 mongoDB 是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51798442/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com