Home > Net >  get id value from req.body.id for new value of object
get id value from req.body.id for new value of object

Time:12-24

I have a field with id value in it. It auto created while created a new data. Concatenated with string. I have doing a try, but it gets undefined value.

  1. code:
const { id, nm_acara, tugas, nm_petugas } = req.body;

const result = await prisma.dinas.create({
  data: {
    kd_dinas: `D-${id}`,
    nm_acara,
    tugas,
    nm_petugas,
    rundown: "/texts/"   req.file.filename,
    },
});
  1. result: result

CodePudding user response:

I think your code is correct

But likely id was not sent properly

So, u should check the existence of id firstly

const { id, nm_acara, tugas, nm_petugas } = req.body;

console.log(id) // print the id

If id still undefined => this issue occurs in client side

CodePudding user response:

Do you sure you sent the id property to the server? or was it just created by the ORM?

CodePudding user response:

  • make sure are you using the below lines for parsing application/json!

    app.use(express.json());

    app.use(express.urlencoded({extended:true}));

  • console.log the req.body for check are you getting id or not.

  • Related