Answer the question
In order to leave comments, you need to log in
What is the correct way to return a function?
The NodeJS server accepts a post request:
router.post('/register', urlencodedParser, function(req, res) {
var regUser, resp;
regUser = new User();
resp = regUser.register(req.body);
console.log(resp);
return res.send(req.body);
});
User = (function() {
function User() {}
User.prototype.register = function(obj) {
var codeFunc, newUser;
codeFunc = 0;
newUser = new userSchema({
login: obj.login,
password: obj.pass,
email: obj.mail
});
userSchema.find({
login: obj.login
}, function(err, docs) {
if (err) {
throw err;
}
if (typeof docs[0] === 'object') {
return console.log('Login already is used', codeFunc = 401);
} else {
return newUser.save((function(_this) {
return function(err) {
if (err) {
throw err;
} else {
codeFunc = 201;
}
return console.log(codeFunc = 201);
};
})(this));
}
});
return codeFunc;
};
return User;
})();
Answer the question
In order to leave comments, you need to log in
Your search is asynchronously performed, and the last return is synchronously processed. Wrap the function in a promise or use async/await.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question