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"});
});
});
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"});
});
}
}
});
Firebase Cloud Functions: PubSub, "res.on is not a function"
TypeError: Cannot read properties of undefined (reading 'createMessageComponentCollector')
I am create a module that is executing tasks based on a config it receivesThese tasks are asynchronous and are returning a promise
This is a really trivial problemI am just curious on how to deal with this in a "professional" manner
Why is the 1st parameter of the require function between the brackets ? What is the difference if there is no brackets ?
I have tried many times to install lwipBut I can't install that