Hello I am new to NodejS and still learning so I might have mistakes and I am trying to learn it by writing code and I am from java background. Asyc functions are still new to me.
Here is my question
I would like to assign user_id to each user from my code.
.post('/register', function (req, res, next) {
var username = req.body.uname;
var password = req.body.psw;
var email = req.body.uemail;
var fullname = req.body.fullname;
var user_id = sql_counts.sql_count()+1;
console.log('Your user id is'+user_id);
sql_connection.User.create({
username:username,
password:password,
email:email,
user_id:user_id
},function (err) {
if(!err){
console.log('Successful');
res.redirect('/registrationComplete');
}else{
console.log('Error creating');
}
});
});
And here is my sql_count function:
function sql_count() {
sql_connection.User.count().then(function (err) {
initCounter =err;
});
return initCounter;
};
I would like to understand more about calls backs since this is very new and confusing to me. Could anyone suggest some materials,links,videos... Although it seems to be understanding in the start I just dont understand it when I try using it. And also how to return values from callbacks to outter function?
Say
In the code given above how do I return value from the 'then' part to outer function sql_count() ?