最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Strapi POST API for content-type with relational fields - Stack Overflow

programmeradmin6浏览0评论

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.

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.

Share Improve this question edited Jul 4, 2023 at 20:14 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Feb 26, 2022 at 11:57 kalexeikalexei 1253 silver badges9 bronze badges 3
  • Can you share the data you are sending to the /api/order route? – nasirmustapha Commented Feb 26, 2022 at 12:01
  • Never mind, I've seen it. In your question you referenced /api/order while in your example code you reference /api/orders is that correct? – nasirmustapha Commented Feb 26, 2022 at 12:02
  • @nasirmustapha yeah it is supposed to be /api/orders, that's the route shown in users permissions – kalexei Commented Feb 26, 2022 at 12:05
Add a ment  | 

4 Answers 4

Reset to default 3

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:

    await axios.post(
      `${baseUrl}/api/orders`,
      {
        data: {
          products: [9],
          total: 320,
          user: 42
        }
      }
    );

i had similair problem and enabling find user under USERS & PERMISSIONS PLUGIN worked for me.

I think the problem is from your route, accessing it with ${baseurl}/orders should work fine.

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:

发布评论

评论列表(0)

  1. 暂无评论