Hello,
Please i have been trying to return data to client on meteor and i get "undefined" always.
I think am mixing up "synchronous and asynchronous" way of outputting response but its driving me crazy here.
Please can someone help out.
here is my client code:
//BASE FUNCTIONS
Template.Home.events({
"submit #login-form": function(){
var formname = 'login-form';
var form = $('form[name='+formname+']');
var username = form.find('input[name=email]').val();
var password = form.find('input[name=password]').val();
var user = {'email':username, 'pass':password};
Meteor.call('AccessUser', user, function(err, resp){
if(err){
console.log(err);
}
console.log(resp);
//FlowRouter.go('/admin');
});
}
});
and here is my method running on server :
Meteor.methods({
//Login User
AccessUser(user){
var que = 'SELECT * FROM users WHERE USERNAME = "'+connection.escape(user.email)+'" AND PASSWORD = "'+connection.escape(user.pass)+'" AND ID = 1';
connection.query(que, function(err, rows){
if(err){
// return err.code;
return err;
}
if(rows.length > 0){
return 1;
}else{
return 0;
}
connection.end();
});
}
});
I never used Meteor, so code below is just result of quick googling:
Meteor.methods({
//Login User
AccessUser(user){
var queryAsync = function(cb) {
var que = 'SELECT * FROM users WHERE USERNAME = "'+connection.escape(user.email)+'" AND PASSWORD = "'+connection.escape(user.pass)+'" AND ID = 1';
connection.query(que, function(err, rows){
if(err){
cb(err);
}
cb(null, rows.length ? 1 : 0);
});
}
var querySync = Meteor._wrapAsync(queryAsync);
return querySync();
}
});
Thanks alot, it worked like magic.
Actually i just moved to meteor too but still very comfortable with mysql cause of the projects we are using it for.
Once again thanks alot
@sidorares please can you explain this line
cb(null, rows.length ? 1 : 0);
Thanks
Okay thanks, found : http://www.w3schools.com/js/js_comparisons.asp.
I did not quickly realize its just js
Most helpful comment
I never used Meteor, so code below is just result of quick googling: