rajamaniraja
8/24/2019 - 7:54 AM

controller.js

const mongoose = require('mongoose');
const User = mongoose.model('User');
const bcrypt = require('bcryptjs');



const signUpController = async (req, res)=> {
  try {
    const phoneNumber = req.body.phoneNumber;
    const password = req.body.password;
    const salt = await bcrypt.genSalt(10);
    const hashedPassword = await bcrypt.hash(password, salt);
    //accesing our cluster  connection from global object
    const dbConnection = await global.clientConnection;
    const db = await dbConnection.useDb("YOUR_CLIENT_DB_IDENTIFICATION_CODE");
    const User = await db.model("User");
    const userPresent = await User.findOne({
            phoneNumber: phoneNumber
        });
    if(userPresent) {
      throw new Error('User Already Present')
    }
    const newUser = await new User({
            phoneNumber: phoneNumber,
            password: hashedPassword
        }).save();
    res.send('Sucess')
  } catch (err) {
    res.status(err.statusCode).send(err.message)
  }
}