M. Straw
M. Straw

Reputation: 502

How can I import relational data when parsing a file in Strapi?

I’m writing a script to import bulk data entries for organizations. In Strapi, however, I have a number of relational fields for the organizations with things like “area_of_focus”, “languages”, etc. My question is, how can I import the data properly for the relational when it’s in a dropdown in the strapi admin panel? I’ve included the section of the code that maps and a screenshot of the relational data for reference.

All the data without a relation imports fine, it’s just any relational field that doesn’t work. I just have a feeling it’s how I’m calling them, but I’m unsure.

Relational Fields:

enter image description here

Mapping Code:

await Promise.all(data.map(entry => 
  strapi.query('organization').create({
    name: entry.name,
    EIN: entry.EIN,
    locations: [
      {
        streetAddress: entry.streetAddress,
        city: entry.city,
        state: entry.state,
        zipCode: entry.zipCode,
        county: entry.county,
        locationType: entry.locationType,
      }
    ],
    contact: [
      {
        firstName: entry.firstName,
        lastName: entry.lastName,
        email: entry.email,
        phone: entry.phone,
        phoneExtension: entry.phoneExtension,
      }
    ],
    description: entry.description,
    webSite: entry.webSite,
    email: entry.orgEmail,
    phone: entry.orgPhone,
    priority_area_topics: entry.priority_area_topics,
    organizationType: entry.organizationType,
    areaOfFocus: entry.organization_area_of_focus,
    localServiceArea: entry.local_service_areas,
    localServiceAreaDescription: entry.localServiceAreaDescription,
    helpDesk: entry.helpDesk,
    officeHours: entry.officeHours,
    maximumResponseTime: entry.maximumResponseTime,
    languages: entry.languages,
  })
));

Upvotes: 2

Views: 285

Answers (1)

dpoerschke
dpoerschke

Reputation: 68

The easiest way to find out what the data structure looks like that Strapi expects is the documentation plugin: https://market.strapi.io/plugins/@strapi-plugin-documentation

After installation, the plugin uses Swagger UI to display the individual services and the structure of the JSON (including relations) that the respective service expects.

Regarding the relations please note that there is a bug in Strapi, see: https://github.com/strapi/strapi/issues/12238 As a workaround you need to add the find-permission to the user / role who is performing the request for the related content type (you want to check first if this is a security issue for your scenario or not - alternatively you might want to try Paratron's approach which is described in the comments).

Upvotes: 1

Related Questions