// Assign unique krypter id
assignKrypterID = () =>
{
let id = Math.floor(Math.random()*(999999-100000)+100000);
console.log(id);
Krypter
.findOne({krypter_id: id.toString()})
.then(krypter => {
if (krypter)
assignKrypterID()
else
return id.toString()
})
.catch(err => console.log(err))
}
const newKrypter = new Krypter({
handle: req.body.handle,
password: req.body.password, // plain text
krypter_id: assignKrypterID()
});
// Hashes the plaintext password and saves it
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(newKrypter.password, salt, (err, hash) => {
if (err) throw err;
newKrypter.password = hash; // encrypted password
newKrypter
.save()
.then(krypter => res.json(krypter))
.catch(err => console.log(err));
})
});
Я пытался назначить уникальный 6-значный идентификатор каждому пользователю во время регистрации с помощью функции assignKrypterID (). Но получается следующая ошибка:

Это моя схема:
const KrypterSchema = new Schema({
handle: {
type: String,
required: true
},
password: {
type: String,
required: true
},
krypter_id: {
type: String,
required: true
}
});
Разве это не может быть достигнуто с помощью рекурсии? Или рекурсия - плохой ход здесь?
Помогите мне, как изменить это, чтобы определить, является ли случайно сгенерированный Id идентичным объекту.