• 主页
  • 序列化插入级联

序列化插入级联

我正在尝试在多个表中插入。

让我来解释一下用户创建一个新客户机,客户机将id插入到账单(idClient)表中

这是有效载荷

{
  "name": "Evelyn",
  "lastName": "Doe",
  "phone": "4534534",
  "email": "eve@hotmail.com",
  "identification": "xxxxx",
  "services": [1, 2, 3],
  "bill":{
    "description": "New project"
  }
}

插入物

_service.create = async (client) => {
 const { description } = client.bill;
 try {
   const data = await Client.create(
    { client, bill: { description: description } },
    { include: { model: Bill } }
   );
  return data.id;
} catch (err) {
    handleError = err.hasOwnProperty("errors")
      ? err.errors.map((error) => error.message)
      : err;
    throw new Error(handleError);
  }
};

但是我得到了这个错误

{
 "name": "Error",
 "message": "ReferenceError: description is not defined"
}

是的,bill表就有这一列。

关系

Client.hasOne(models.Bill, { foreignKey: "idClient" });

因此,我被卡住了,我阅读了文档,并试图按照他们所做的那样做,但我不知道我做错了什么

https://sequelize.org/master/manual/creating-with-associations.html

转载请注明出处:http://www.jxbyjx.net/article/20230526/1622272.html