Reputation: 47
I have a file signupResponse.js
const foundRegisterUser = function(err, foundUser){
if(!err){
console.log("Não tivemos erros")
if(!foundUser){
if(req.body.password == req.body.passwordConfirmation){
User.register({username: req.body.email}, req.body.password, function(err, user){
if (err){/
console.log(err);
} else {
console.log("Success")
res.render("welcome")
}
})
} else {
res.render("registerFailure",{msg0:MSG_INVALID_PASSWORD, msg1:"", msg2:""})
}
} else {
res.render("registerFailure",{msg0:MSG_INVALID_USER, msg1:"", msg2:"" })
}
} else {
res.render("registerFailure",{msg0:MSG_ERROR_REGISTER, msg1:"", msg2:""})
}
}
const signupresponse = (req, res) => {
Register.findOne({username: req.body.email}, function(err, foundUser){
if(!err){ /
if(!foundUser){
console.log("Usuário não encontrado")
res.render("registerFailure",{msg0:MSG_NOT_FOUND_REGISTER_0, msg1:MSG_NOT_FOUND_REGISTER_1, msg2:MSG_NOT_FOUND_REGISTER_2 })
}
else {
User.findOne({username: req.body.email},
foundRegisterUser(err, foundUser)
)
}
} else {
res.render("registerFailure",{msg0:MSG_ERROR_REGISTER, msg1:"", msg2:"" })
}
})
}
module.exports = signupresponse
I try to export to app.js
const signupResponse = require("./funcoes/signupResponse")
app.post("/signup", signupResponse)
This return ReferenceError: res is not defined But when I put everything inside signupResponse and exports again, this works well:
const signupresponse = (req, res) => {
Register.findOne({username: req.body.email}, function(err, foundUser){
if(!err){
if(!foundUser){
console.log("Usuário não encontrado")
res.render("registerFailure",{msg0:MSG_NOT_FOUND_REGISTER_0, msg1:MSG_NOT_FOUND_REGISTER_1, msg2:MSG_NOT_FOUND_REGISTER_2 })
}
else {
User.findOne({username: req.body.email}, function(err, foundUser) {
if(!err){
console.log("Não tivemos erros")
if(!foundUser){
if(req.body.password == req.body.passwordConfirmation){
User.register({username: req.body.email}, req.body.password, function(err, user){
if (err){/
console.log(err);
} else {
console.log("Success")
res.render("welcome")
}
})
} else {
res.render("registerFailure",{msg0:MSG_INVALID_PASSWORD, msg1:"", msg2:""})
}
} else {
res.render("registerFailure",{msg0:MSG_INVALID_USER, msg1:"", msg2:"" })
}
} else {
res.render("registerFailure",{msg0:MSG_ERROR_REGISTER, msg1:"", msg2:""})
}
})
}
} else {
res.render("registerFailure",{msg0:MSG_ERROR_REGISTER, msg1:"", msg2:"" })
}
})
}
module.exports = signupresponse
The app works good and returns no errors. I don't know how to use module.exports when has a lot of function to export. How I export this signupResponse and put another function inside it without error? Thanks!
Upvotes: 0
Views: 46
Reputation: 898
It is not a problem of export
, but a problem of scope of the ´´res´´ variable.
In the first code example, you have this error because you try to access the res
object inside foundRegisterUser
but this function doesn't know the res
object. You define a res
object in your other funtion signupresponse
, ant the res object exist only inside of it. foundRegisterUser
does not have any knowledge of it.
It works in your second example because you do not have a problem of scope for res
in this case: everything is inside the same function and then your callback has access to
the res
object.
One way to make your first example work would be to put the definition of foundRegisterUser
inside signupresponse
like this:
const signupresponse = (req, res) => {
const foundRegisterUser = function(err, foundUser){ ... };
Register.findOne({username: req.body.email}, function(err, foundUser){...
It would not be the prettiest, and would let plenty of room for structure improvement, but it should work.
Upvotes: 1