0

Main function

export async function postData(TVMID) {
const data = await FetchingData(TVMID)

  // regex the year
  const rex = /^[0-9]{4}/g;
  let country;
  let network;

  
  let geners = await CheckGenderIfExist(data.genres).then(genx => genx)

more code....

}

Check if data exist function

export async function CheckGenderIfExist(genders) {

  let listOfGenders = [];

  if (Array.isArray(genders)) {
    genders.forEach(gen => {
      axios.get(`${process.env.NEXT_PUBLIC_API_URL}/api/genders?filters[$and][0][Name][$eq]=${gen}`)
        .then((response) => {
          if (response.data.data.length == 0) {
            AddTVGenders(gen)
          } else {
            listOfGenders.push(response.data.data[0].id)
          }
        });
    })
  } else {
    axios.get(`${process.env.NEXT_PUBLIC_API_URL}/api/genders?filters[$and][0][Name][$eq]=${genders}`)
      .then((response) => {
        listOfGenders.push(response.data.data[0].id)
      });
  }

  return listOfGstenders;
}

Add missing data function

export async function AddTVGenders(gen) {

  await axios.post(`${process.env.NEXT_PUBLIC_API_URL}/api/genders`, {
    headers: {
      Authorization: `Bearer ${process.env.API_TOKEN}`,
    },
    "data": {
      Name: gen,
    }
  })

  CheckGenderIfExist(gen)

}

Im' using strapi v4.5 with nextjs v13, while tring to post data for some of the relation fields , i have to check if the data exist if not, i create it then get the id then add it to the main post function This is what i came up with but it return empty array

spec10
  • 23
  • 4

0 Answers0