每次我尝试通过 /api/orders 创建订单时,它都会给我 400 (错误请求),似乎没有关于如何使用关系字段创建记录的正确清晰解释,这是我发现的唯一接近的解释我需要的是这样的:
所以据说我应该根据关系类型使用 id 或 id 列表,但它仍然给我 400 Bad request 且没有任何解释在回应中。
我的订单内容类型如下所示:
用户是多对一的关系,所以一个用户可以有多个订单,但每个订单只能有一个用户,而产品是One- to-Many,所以一个订单可以有很多产品
这就是我的 API 调用的样子:
await axios.post(
`${baseUrl}/api/orders`,
{
products: [9],
total: 320,
user: 42
}
);
产品和用户 ID 正是我在数据库中的产品和用户 ID,身份验证不是问题。
请帮助我理解我做错了什么以及我应该如何使用关系字段创建记录。
Every time I try to create an order via /api/orders it gives me 400 (Bad request), there doesn't seem to be a proper clear explanation anywhere on how to create records with relational fields, the only one I found close to what I needed was this:
So supposedly I should use an id or a list of ids depending on the type of relation, but it still gives me 400 Bad request with no explanation in the response.
My order content-type looks like this:
User is a Many-to-One relationship, so a user can have many orders, but there can only be one user per order, and products is One-to-Many, so an order can have many products
This is what my API call looks like:
await axios.post(
`${baseUrl}/api/orders`,
{
products: [9],
total: 320,
user: 42
}
);
The products and user ids are exactly the ones I have in the database and authentication is not the problem.
Please help me understand what I'm doing wrong and how I should be creating records with relational fields.
发布评论
评论(4)
好吧,显然它确实给了我一个描述问题的正确响应,只是我必须转到浏览器中的“网络”选项卡并在那里找到它,这是最愚蠢的事情,我所要做的就是这样:
Ok, apparently it does give me a proper response describing the problem, it's just I had to go to the Network tab in the browser and find it there, and it's the stupidest thing, all I had to do it is this:
我遇到了类似的问题
在 USERS & 下启用查找用户权限插件对我有用。
i had similair problem and
enabling find user under USERS & PERMISSIONS PLUGIN worked for me.
我认为问题出在您的路线上,使用 ${baseurl}/orders 访问它应该可以正常工作。
I think the problem is from your route, accessing it with ${baseurl}/orders should work fine.
如果您无法更新引用,即使使用正确的 JSON 结构(如此处提到的结构),请检查授予的权限引用的实体。
检查是否已授予更新和创建权限,如下所示:
In the case you cannot update a reference even with the right JSON structure like the one mentioned here, then check the permissions given to the referenced entity.
Check that the update and create permissions are granted like here: