skip to Main Content
import express from"express"
import cors from "cors"
import mongoose from "mongoose"
import bodyParser from'body-parser'
// const {Schema} = mongoose;
const app = express()
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: true}))
app.use(cors())


await mongoose.connect('mongodb://127.0.0.1:27017/loginRegisterDB',{
    useNewUrlParser: true,
    useUnifiedTopology: true
},)
    .then(() => console.log( "connected"))
    .catch((err) => {console.error(err);});

const userSchema = new mongoose.Schema({
    firstName: String,
    lastName: String,
    email: String,
    password: String,
    reEnterPassword: String,
    dob: Number,
    phone: Number,
    address: String,
    pincode: Number
})

const User = new mongoose.model("User", userSchema)

app.post("/login", (req, res)=>{
    const { email, password } = req.body
    User.findOne({ email: email},(err, user) =>{
        if(user){
            if(password === user.password){
                res.send({message: "Login Successful", user: user})
            }else{
                res.send({message:"Password not match"})
            }
        }else{
            res.send({message:"User not register"})
        }
    })
})

app.post("/register", (req, res)=>{
    // console.log(req.body)
    const { firstName, lastName, email, password, reEnterPassword, dob, phone, address, pincode } = req.body;
    User.findOne({email: email}, (err, user) => {
        if(user){
           res.send({message: "User alredy registered"})
        } else {
        const user = new User({
            firstName,
            lastName,
            email,
            password,
            reEnterPassword,
            dob, 
            phone,
            address,
            pincode
        })
        user.save(err => {
            if(err) {
                 res.send(err)
            } else{
                res.send({ message: "Successfully Registered, Login Now"})
            }

            })
        }  
    })
})

app.listen(8001,() => {
       console.log("started at port 8001")
})```

i get this error

MongooseError: Model.findOne() no longer accepts a callback
at Function.findOne (D:codeProjectLogin-Registrationbackendnode_modulesmongooselibjs:284:15
at Function.process_params (D:codeProjectLogin-Registrationbackendnode_modulesexpresslibrouterindex.js:346:12) at next (D:codeProjectLogin-Registrationbackendnode_modulesexpresslibrouterindex.js:280:10) at cors (D:codeProjectLogin-Registrationbackendnode_modulescorslibindex.js:188:7)

2

Answers


  1. You no longer use callbacks, it returns a promise.
    You can do:

    try {
      const user = await User.findOne({ email: email});
      if(password === user.password) {
        res.send({message: "Login Successful", user: user})
      } else {
        res.send({message:"Password not match"})
      }
    } catch(e) {
        res.send({message:"User not register"})
    }
    
    Login or Signup to reply.
  2. You can use the then and catch block.

    User.findOne({email: email}).then((user) => {
     if(!user) {
         return res.send({message: "User not register"});
     }
     if(password === user.password){
         return res.send({message: "Login Successful", user: user});
     }else{
         return res.send({message: "Password not match"});
     }
    })
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search