Home > database >  why "if" condition is ignored?
why "if" condition is ignored?

Time:02-10

when I run this server on postman, I'm getting "error login" but it should return "success". but when i do add console.log(body.req) then on terminal I'm gettting "name, email,password", which i passed in body.

 const bodyParser=require('body-parser');   
 const app=express();
  app.use(bodyParser.json());
  app.use(express.urlencoded({ extended: true }));
  app.use(express.urlencoded({ extended: false}));

 const database= {
     users : [
         {
          id: '123',
          name: 'amit',
          email :'[email protected]',
          password:'123',
          entries:'0',
           joined: new Date()
         },
         /*{
            id: '124',
          name:'Rahul',
          email :'[email protected]',
          password:'54858',
          entries:'0',
           joined: new Date() 
         },*/
     ]
     
 }
   /*const app=express();
   app.get('/',(req,res)=>{
        res.send("this is working");
   })*/
    app.post('/signin',(req,res)=>{
         //console.log(req.body);
      if(req.body.name===database.users[0].name && req.body.email===database.users[0].email) {
           console.log("sucess");
      }
       else {
           res.status(400).json("error login");
       }
    })
   app.listen(3000,()=>{
        console.log("app is running on port");
   })

CodePudding user response:

First of all you don't appear to be sending a response in the if condition. What this will do, if the control went to your if condition, is print 'success' on console, but not send any response (i.e. hang the request).

Secondly, is this exactly the code you ran? Did you have password check in the if condition as well? Do note that when using ===, '123' is not equal to 123.

These are the only clues, I can provide from what was shared. You can share the postman request body, which might have more clues.

CodePudding user response:

yes what you said in the first point is more likely to happen but why this enter image description herenot is printing on postman "success"

  • Related