Can´t set headers after they are send checking for email and name

310
January 31, 2017, at 9:15 PM

in my projet i want to build a store tha has some products, to do that i defined 2 schemas a store(loja) schema and a product schema(produto), i want to post the data from req.body and check it with postman, i want to check if the email and the name already exists for that store in the database, and is there where the app fails and send me a error:

CanĀ“t set headers after they are send checking

i have no idea why that happen if someone can explain me i appreciate. **here is the code****

  router.post('/registo',function(req,res){
  if(JSON.stringify(req.body) == "{}"){
        return res.status(400).json({Error:"empty request body"});
    }
    if(!req.body.email && !req.body.nome){
        return res.status(400).json({Error:"you need to specify the email and the name"});
    }
    if(!req.body.email){
        return res.status(400).json({Error:"you need to specifiy the email"});
    }
    if(!req.body.nome){
        return res.status(400).json({Error:"you need to specify the name"});
    }
    Loja.find({nome: req.body.nome},function(err,loja){
      if(err){
        return res.status(500).json({Error:"database problem"});
      }
      if(loja){
        return res.status(400).json({Error:"Ja existe uma loja com o nome que especificou"});
      }
    });
    Loja.find({email: req.body.email},function(err,loja){
      if(err){
        return res.status(500).json({Error:"database problem"});
      }
      if(loja){
        return res.status(400).json({Error:"ja existe uma conta com o email que especificou"});
      }
    });
    var loja = new Loja(req.body);
    loja.save(function(err){
        if(err){
            return res.status(500);
        }
        return res.json({message: "Contact added"});
    });
});
Answer 1

The problem with your code is that there are multiple if statements in one request function. Basically, the program is returning values multiple times. To prevent this from happening, try enclosing if statements into each other to make sure it won't return multiple values.

Try restructuring code like this:

router.post('/registo',function(req,res){
 if(JSON.stringify(req.body) == "{}"){
       return res.status(400).json({Error:"empty request body"});
 }else{
   if(!req.body.email && !req.body.nome){
       return res.status(400).json({Error:"you need to specify the email and the name"});
   }
   elseif(!req.body.email){
       return res.status(400).json({Error:"you need to specifiy the email"});
   }
   elseif(!req.body.nome){
       return res.status(400).json({Error:"you need to specify the name"});
   }else{
     Loja.find({nome: req.body.nome},function(err,loja){
       if(err){
         return res.status(500).json({Error:"database problem"});
       }
       if(loja){
         return res.status(400).json({Error:"Ja existe uma loja com o nome que especificou"});
       }
     });
     Loja.find({email: req.body.email},function(err,loja){
       if(err){
         return res.status(500).json({Error:"database problem"});
       }
       if(loja){
         return res.status(400).json({Error:"ja existe uma conta com o email que especificou"});
       }
     });
     var loja = new Loja(req.body);
     loja.save(function(err){
         if(err){
             return res.status(500);
         }
         return res.json({message: "Contact added"});
     });
   }

 }

});
Rent Charter Buses Company
READ ALSO
How to identify results in Promise.all()

How to identify results in Promise.all()

I am create a module that is executing tasks based on a config it receivesThese tasks are asynchronous and are returning a promise

382
Different variable name case convention in one application

Different variable name case convention in one application

This is a really trivial problemI am just curious on how to deal with this in a "professional" manner

378
Javascript String between the brackets or not?

Javascript String between the brackets or not?

Why is the 1st parameter of the require function between the brackets ? What is the difference if there is no brackets ?

494
How to install lwib node package for resizing and cropping images?

How to install lwib node package for resizing and cropping images?

I have tried many times to install lwipBut I can't install that

361