Api testing is failed on postman

I’m testing my Api on postman but it throws an error.

const router= express.Router();
const User= require('../../Models/User');

router.get('/test', (req,res) =>{
    res.json({msg:"This is a user route"});
});

 // Check to make sure nobody has already registered with a duplicate email
router.post('/register', (req,res)=>{
  User.findOne({ email:req.body.email})
    .then(user =>{
        if(user){
     // Throw a 400 error if the email address already exists
        return res.status(400).json({email:"A user is already registered with that email"})
        } else{
            const newUser = new User({
                handle:req.body.handle,
                email:req.body.email,
                password:req.body.password
            })
            /*bcrypt.genSalt(10,(err,salt)=>{
                bcrypt.hash(newUser.password, salt, (err,hash) =>{
                    if(err) throw err;
                    newUser.password=hash;
                })*/
           // }
    //)
            newUser.save()
            .then(user => res.send(user))
            .catch(err => res.send(err)); //take this for testing
        }
});
});
  
module.exports = router;  ```

``` const express = require ('express');
//const bodyParser = require('body-parser');
const app= express();
const mongoose = require('mongoose');
const db = require('./config/keys').mongoURI;
const users = require("./routes/api/users");
//const tweets = require("./routes/api/tweets");
const User= require('./Models/User');

mongoose
.connect(db, { useNewUrlParser: true ,useUnifiedTopology: true})
.then(() => console.log("Connected to MongoDB successfully"))
.catch(err => console.log(err));

app.get('/', (req,res) => {
 const user = new User({
    handle: 'john',
    email: "john@john.john",
    password: "john1234"
})
 user.save()
 res.send('Hello world');
});
//middleware
app.use("/api/users", users);
//app.use("/api/tweets", tweets);


//app.use(bodyParser.urlencoded({ extended: false }));
//app.use(bodyParser.json())



const port = process.env.PORT || 5000;

app.listen(port, () => console.log(`Server is running on port ${port}`));

Show the body-parser

@jenovs I already show it But it didn’t work.

Body parser must be before you access body. And currently it’s commented out.

Body-parser middleware is what attaches the body property to the express request object for you to use.

Right now you are not using the body-parser middleware. Uncomment the related lines.

@jenovs @ofk8vb it is still throwing the same error.

const express = require ('express');
const bodyParser = require('body-parser');
const app= express();
const mongoose = require('mongoose');
const db = require('./config/keys').mongoURI;
const users = require("./routes/api/users");
//const tweets = require("./routes/api/tweets");
const User= require('./Models/User');

mongoose
.connect(db, { useNewUrlParser: true ,useUnifiedTopology: true})
.then(() => console.log("Connected to MongoDB successfully"))
.catch(err => console.log(err));

app.use(bodyParser.urlencoded({ 
    extended: false }));
    
app.use(bodyParser.json());

app.get('/', (req,res) => {
 const user = new User({
    handle: 'john',
    email: "john@john.john",
    password: "john1234"
})
 user.save()
 res.send('Hello world');
});
//middleware
app.use("/api/users", users);
//app.use("/api/tweets", tweets);




const port = process.env.PORT || 5000;

app.listen(port, () => console.log(`Server is running on port ${port}`)); ```

@jenovs @ofk8vb I fixed this issue by using bcryptJS. Thanks

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