Seeding data to database using Strapi headless CMS

Viewed 3002

I am fairly new to using Strapi the headless CMS. I am trying to populate some data into my database, just like using knex with node js, however, I am not sure about doing this. I have seen a tutorial where inside the config/functions/bootstrap.js, we would module.export an asynchronous function that will populate the data; I have defined my code like so:

"use strict";
const data = [
 {
   title: "string",
   description:"string",
   director: "string",
   assistantDirector: "string",
   directorOfPhotography: "string",
   firstAc: "string",
   sound: "string",
   thumbnail: "",
   images: "",
   hairAndMakeUp: "string",
   productionAssistant: "string",
   writer: "string",
   cast: "string",
   video: "string",
   dateFilmed: 2020,
 }];

module.exports = async () => {
  data.forEach(reel => {
    await strapi.services.reel.create({
      title: reel.title,
      description: reel.description,
      director: reel.director,
      assistantDirector: reel.assistantDirector,
      directorOfPhotography: reel.directorOfPhotography,
      firstAc: reel.firstAc,
      sound: reel.sound,
      hairAndMakeUp: reel.hairAndMakeUp,
      productionAssistant: reel.productionAssistant,
      writer: reel.writer,
      cast: reel.cast,
      video: reel.video,
      dateFilmed: reel.dateFilmed,
    });
  })
};

However, after running strapi develop I get a Error while running command develop: undefined error. What am I missing?

Thank you for your time.

1 Answers

Problem is solved.

It turns out that removing the async/await did the trick. I realized the function didn't even have to be asynchronous. Not really sure why this happened but here is the end result:

"use strict";
const data = [
 {
   title: "string",
   description:"string",
   director: "string",
   assistantDirector: "string",
   directorOfPhotography: "string",
   firstAc: "string",
   sound: "string",
   thumbnail: "",
   images: "",
   hairAndMakeUp: "string",
   productionAssistant: "string",
   writer: "string",
   cast: "string",
   video: "string",
   dateFilmed: 2020,
 }];

const seedReel = (data) => {
  data.forEach((reel) => {
    strapi.query("reel").create({
      title: reel.title,
      description: reel.description,
      director: reel.director,
      assistantDirector: reel.assistantDirector,
      directorOfPhotography: reel.directorOfPhotography,
      firstAc: reel.firstAc,
      sound: reel.sound,
      hairAndMakeUp: reel.hairAndMakeUp,
      productionAssistant: reel.productionAssistant,
      writer: reel.writer,
      cast: reel.cast,
      video: reel.video,
      dateFilmed: reel.dateFilmed,
    });
  });
};

module.exports = seedReel(data)
Related