检查值是否存在然后在循环中创建新值?

时间:2018-06-10 14:00:45

标签: javascript node.js mongoose

我试图创建一个检查输入值是否存在的函数,如果存在,则创建新的函数,然后再次检查以确保新创建的值也不存在。我能够处理检查部分,但我现在仍然坚持创造新的价值部分。以下是我的代码,谢谢你的帮助

exports.handleCheckExist = (req, res, next) => {
const username = req.body.username;
User.mongooseEN
.findOne({ username: { $regex: username, $options: "i" } })
.then(result => {
  if (result) {
      let newUsername =
        username.replace(/\s+/g, "").substring(0, 5) +
        "_" +
        Date.now().toString(36);
      req.body.username = newUsername

      handleCheckExist //Is there a way to run the process again here?
  } else {
     res.status(200).json({
      status: "created new user"
    });
  }
})
.catch(err => {
  res.status(400).json(err);
});
}

0 个答案:

没有答案