Return insertId from an INSERT query

3,031 views
Skip to first unread message

yauri

unread,
Jul 6, 2013, 8:13:20 AM7/6/13
to node-...@googlegroups.com
Hi, I have a problem with getting last insert id from a query.
Please take a look on the codes below

function Instructor(){}

Instructor.prototype = {
create: function(data) {
    connection.db.getConnection(function(err, conn){
      conn.query("INSERT INTO instructor SET ?", data, function(err,result){
        if(err) {
          throw err;
        }
        conn.end();
        console.log(result.insertId) //this is return a correct id
        return result.insertId; //this will return nothing
      });
    })
}
var data = {
    name: 'name',
    mail: 'ma...@gmail.com',
    phone: '12345'
}
var instructor = new Instructor();
var id = instructor.create(data);

Why id is undefined?

// ravi

unread,
Jul 6, 2013, 9:17:34 AM7/6/13
to node-...@googlegroups.com
You are getting tripped up by Node’s async’ness. Note that there are two (three actually) functions above: the one you call is create(), which kicks off an async call to the DB and returns back to your calling point, *without* any return value.

The second function, the callback to the DB query, where the results arrive, does return the insertId but to whom?

You will have to rethink your flow (pass a callback function where to instructor.create() and call that after conn.end() passing it the insertId), or use Promises or an Async library.

—ravi


yauri

unread,
Jul 6, 2013, 11:41:11 AM7/6/13
to node-...@googlegroups.com
Thanks ravi!!
I modified the function with callback and I can get the returned id! I'm new with javascript, in learning process :D

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 :)

// ravi

unread,
Jul 6, 2013, 12:23:52 PM7/6/13
to node-...@googlegroups.com
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.
>
>

yauri

unread,
Jul 6, 2013, 1:13:16 PM7/6/13
to node-...@googlegroups.com
Thanks for your information about this Pyramid of Doom!
So many things to learn about Javascript... 
Reply all
Reply to author
Forward
0 new messages