0

Im working on web application which is based on MEAN Framework. I use Node.JS & Express.Js server side, MongoDB as Database and AngularJS on the front end.

I am creating a support ticket system on my site. So if a user has any issues they can start a support ticket, When they fill out the form to create ticket and submit the form I want to generate a sequential ID for that users ticket.

The Rules:

The ID's should be plain number starting from 5000 and ongoing (i.e 5001, 5002, 5003, ..... n)

When ever a new ticket is generated, the system should check the last support ticket ID and then increment it by One (i.e latest ID = 5033, new ID = 5044. which means now the latest ID is 5044)

What I have tried:

I thought about creating a module in Node.Js which would contain the latest ID. And I will get that ID on each ticket generation & increment it by ONE and update the latest ID in the module.

// config/supportTicket

module.exports = {
    latestTicketID : 5059
};

And then I export the module into my routes where I try to increment the ID on form submit.

  newTicket.ticketID = supportTicket.latestTicketID + 1;

  // save the ticket
  newTicket.save(function(err) {
    if (err){
              res.send(err);
            }
              res.json(newTicket);
  });

Questions:

How can update the update in my module to the latest incremented value? Currently I can save an incremented value in the database but the ID value in module is not updated.

Also is this the right approach? is their a better way of achieving this?

Any help will be appreciated.

Skywalker
  • 4,984
  • 16
  • 57
  • 122
  • MongoDB core documentation: [Create an Auto-Incrementing Sequence Field](https://docs.mongodb.org/manual/tutorial/create-an-auto-incrementing-field/). It must be a good option. Others are suggesting it too. – Blakes Seven Mar 10 '16 at 09:55
  • Refer to this : Refer to this : https://docs.mongodb.org/manual/tutorial/create-an-auto-incrementing-field/ – SiddAjmera Mar 10 '16 at 09:56

0 Answers0