Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

100
Views
How to get back the object id after POST method using Sequelize and send it to frontend?

I try to post a new object in my MySQL database in React using Sequelize and I have this route:

app.post("/event", checkAuthorization, async (req, res) => {

    let new_event = {
        title: req.body.title,
        description: req.body.description,
        label: req.body.label,
        day: req.body.day,
        user_id: req.body.user_id,
    };

    let response = {};

    Events.create(new_event)
        .then(() => {
            response.msg = 'Adaugat cu succes!';
            response.added = 1;
            res.send(response);
        })
        .catch((err) => {
            response.msg = err;
            response.added = 0;
            res.send(response);
        });

});
  • checkAuthorization is to check if user is logged id

My problem is: I send an object without an id because is auto-increment in my database and all I want is after I post this object in database, to return to frontend the object id created by database and I don't know how to write that.

Can somebody help me, please?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

This should help you:

Events.create(new_event)
        .then((event) => {
            response.id=event.id
            response.msg = 'Adaugat cu succes!';
            response.added = 1;
            res.send(response);
        })
        .catch((err) => {
            response.msg = err;
            response.added = 0;
            res.send(response);
        });

you will get the created object as a parameter

about 4 years ago · Juan Pablo Isaza Report

0

I tried to used:

Events.create(new_event)
        .then((event) => {
            console.log(event);
            response.msg = 'Adaugat cu succes!';
            response.added = 1;
            res.send(response);
        })
        .catch((err) => {
            response.msg = err;
            response.added = 0;
            res.send(response);
        });

And this is the output:

event {
  dataValues: {
    id: null,
    title: 't',
    description: 'dsada',
    label: 'green',
    day: 1645653600000,
    user_id: 2
  },
  _previousDataValues: {
    title: 't',
    description: 'dsada',
    label: 'green',
    day: 1645653600000,
    user_id: 2,
    id: null
  },
  uniqno: 1,
  _changed: Set(0) {},
  _options: {
    isNewRecord: true,
    _schema: null,
    _schemaDelimiter: '',
    attributes: undefined,
    include: undefined,
    raw: undefined,
    silent: undefined
  },
  isNewRecord: false,
  null: 23
}

That 'null: 23' is my id and I don't know why database returns me the id like that... Can someone explain me?

about 4 years ago · Juan Pablo Isaza Report

0

You will get easily user inserted data using below code

const result = await Events.create(new_event);
console.log(result.id);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!