【问题标题】:Unable to Reference new Strapi Entry无法引用新的 Strapi 条目
【发布时间】:2021-10-13 02:02:08
【问题描述】:

我有以下代码...

const createThenReference = async () => {
return await axios.post('http://localhost:1337/firsts', {
  name: "Jeremy"
})
.then(async (res)=>{
  await axios.post('http://localhost:1337/seconds', {
    name: "Jonathan",
    first: res.data
  })
  .then((res)=>{console.log(res)})
  .catch((err)=>{console.log(err)})
})
.catch((err)=>{console.log(err)})
  }

每个表的字段如下...

First = { name: String, second: Relationship }
Second = { name: String, first: Relationship }

我想要实现的是,在创建 firsts 条目后,会在 seconds 内创建一个新条目,并引用回 firsts 在 1 对 1 引用中。

我已经成功地引用了已经以类似方式创建的项目,只需将它们传递到有效负载中的相应字段。但是,当它最近创建时,我遇到了问题。 -- 我找不到任何文档甚至谈到从前端创建引用。

【问题讨论】:

    标签: strapi


    【解决方案1】:

    显然,您可以简单地传入与该字段有关系的表条目的 ID。 -- 这很奇怪,因为我记得之前尝试过但没有成功。

    我变了……

    await axios.post('http://localhost:1337/seconds', {
        name: "Jonathan",
        first: res.data
      })
    

    await axios.post('http://localhost:1337/seconds', {
        name: "Jonathan",
        first: res.data.id
      })
    

    而且关系建立得很好。

    有关更多信息,请查看这篇 Strapi 帖子Understanding and using Relations in Strapi

    【讨论】:

      猜你喜欢
      • 2019-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 2021-06-12
      相关资源
      最近更新 更多