So I have this MEAN-stack app where, on the back-end, I have two ways to create a new user: through google authentication and through username.
Now, I am working on the code to create a new user through username, but I can't return the value from another function in my node.js file (in this case createNewUser()). When I look at my database the new user is created, but the value returned from createNewUser() is undefined.
I first thought it was a problem due to asynchronicity, so I tried a setTimeout(). The function however still returns undefined. Does someone see why?
This is the route in my node.js backend: the two console.logs return undefined
router.post('/signup', function(req, res){
var result = createNewUser(req.body);
console.log('result 1', result);
setTimeout(function(){
console.log('result2', result);
if (!result.success){
res.status(500).json({
message: "unable to create User",
obj: result.err
})
} else {
res.status(200).json({
message: "successfully created new user",
obj: result.obj
})
}
}, 6000);
});
This is the createNewUser()
function in that same file, the console.log returns the user, so the user is actually created, the function just doesn't return the value.:
function createNewUser(userData, socialRegistry){
var user;
if (socialRegistry){
user = new User({
firstName: userData.firstName,
lastName: userData.lastName,
email: userData.body.email,
googleID: userData.body.provider.ID
});
} else {
user = new User({
firstName: userData.firstName,
lastName: userData.lastName,
username: userData.username,
password: bcrypt.hashSync(userData.password, 10),
email: userData.email,
registerDate: new Date()
});
}
user.save(function(err, user) {
if (err) {
return {
success: false,
err: err
}
}
if (socialRegistry){
return user
} else {
console.log(user);
var test = {
success: true,
obj: user
}
return test
}
});
}