从 req.body.id 获取对象新值的 id 值

get id value from req.body.id for new value of object

我有一个包含 id 值的字段。它在创建新数据时自动创建。与字符串连接。我试过了,但它得到了 undefined 值。

  1. 代码:
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. 结果:

我认为你的代码是正确的

但可能 id 发送不正确

所以,你应该先检查id是否存在

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

console.log(id) // print the id

如果id仍然undefined => 这个问题发生在客户端

您确定将 ID 属性 发送到服务器了吗?或者它只是由 ORM 创建的?

确保您使用以下行进行解析 application/json!?

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

console.log req.body 检查你是否获得了 id。