First time it saves client to database and second time it gives 500 error

 Client.findOne({ email: req.body.email }, function (err, user) {
        console.log(user);
   
      // Make sure user doesn't already exist
      if (user) return res.status(400).send({ msg: 'The email address you have entered is already associated with another account.' });
   
      // Create and save the user
     Client.create({ username: req.body.name, email: req.body.email, phone: req.body.phone, password: req.body.password }, function(err, client) {
          if (err) { return res.status(500).send({ msg: err.message }); }
          console.log(client);

          // Create a verification token for this user
          var token = new Token({ _userId: client._id, token: crypto.randomBytes(16).toString('hex') });
   
          // Save the verification token
          token.save(function (err) {
              if (err) { return res.status(500).send({ msg: err.message }); }
});

What is Client? what is Token? Where are you getting these object definitions from? What kind of database are you writing to?

I have defined schema definitions in another file then I am creating instance of them.

The problem got solved it occurs due to duplicate indexes.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.