On Jul 6, 2013, at 11:41 AM, yauri <
yaurie...@gmail.com> wrote:
> Thanks ravi!!
> I modified the function with callback and I can get the returned id! I'm new with javascript, in learning process :D
>
You are welcome. Getting comfortable with Node’s async + JavaScript’s FP roots is a tricky business for sure. Before you fall prey to the callback pyramid of doom (
http://twitpic.com/6rlqb5) consider an option like Promises (I suggest Q:
https://github.com/kriskowal/q) or a flow library (I suggest Flow:
https://github.com/willconant/flow-js). But I would also caution against letting these code-tamers prevent/protect you from understanding and getting comfortable with Node async and JavaScript closures.
2 cents,
—ravi
> function Instructor(){}
>
> Instructor.prototype = {
> create: function(data, callback) {
> connection.db.getConnection(function(err, conn){
> conn.query("INSERT INTO instructor SET ?", data, function(err,result){
> if(err) {
> throw err;
> }
> conn.end(function(){
> callback(result.insertId); //this will return nothing
> });
> });
> })
> }
> var data = {
> name: 'name',
> mail: '
ma...@gmail.com',
> phone: '12345'
> }
> var instructor = new Instructor();
> instructor.create(data,function(id){
> console.log(id); // I got the ID!
> });
>
> Thanks again :)
>
> --
> You received this message because you are subscribed to the Google Groups "node-mysql" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
node-mysql+...@googlegroups.com.
> To post to this group, send email to
node-...@googlegroups.com.
> Visit this group at
http://groups.google.com/group/node-mysql.
> For more options, visit
https://groups.google.com/groups/opt_out.
>
>