Muzammil Naseer Muzammil Naseer - 6 months ago 62
Node.js Question

Nodjs sequalize how to get last insert id

I am using Sequalize with nodejs to insert a row in a table,now i want that last inserted id,i want some thing like "mysql_insert_id()", here is my code:

function registerAgent(agent_data,request_key,res,next)
{
models.agent.create({creator_id: agent_data.userid }, { fields: [ 'creator_id'] }).then(function(user) {
console.log(user);
});
};

Answer

The create() return the last insert element, so the last id is user.id. Is this for your problem?

function registerAgent(agent_data,request_key,res,next)
{
  models.agent.create({creator_id: agent_data.userid },  { fields: [ 'creator_id'] }).then(function(user) {
      console.log(user.id);
    }); 
};