Seeding data to database using Strapi headless CMS
Asked Answered
F

2

6

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.

Foskett answered 8/8, 2020 at 14:31 Comment(2)
What node version are you running? What Strapi version are you running?Frunze
@ghosh, thank you for your reply. But I actually solved the problem.Foskett
F
4

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)
Foskett answered 10/8, 2020 at 7:50 Comment(2)
This is a classic example of when not to use arrow functions. For the very least make sure your constants and function arguments have different names.Trenatrenail
Don't see how arrow functions should make a difference. Yes, you're right that having a parameter coming in called "data" as well as a global "data" array is a recipe for disaster. As they've said, it's the "async" that was the issue, but one problem here is that they've changed their code in a non-meaningful way between the question and answer.Sula
L
2

Update for everyone that's using newest Strapi V4:

Inside src/index.js edit bootstrap function:

async bootstrap({ strapi }) {
    
    // For deleting all entries
    // await strapi.db.query("api::reel.reel").deleteMany({
    //   where: {
    //     id: {
    //       $null: false,
    //     },
    //   },
    // });

    // For creating entries
    // const chunkSize = 500;
    // for (let i = 0; i < data.length; i += chunkSize) {
    //   const chunk = data.slice(i, i + chunkSize);
    //   await strapi.db.query("api::reel.reel").createMany({
    //     data: chunk,
    //   });
    // }
  }

If you are not sure what your service is called you can find it by running npm run strapi services:list

Documentation: https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/query-engine/bulk-operations.html#createmany

Landlubber answered 13/11, 2022 at 17:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.