Baboo7 / strapi-plugin-import-export-entries

Import/Export data from and to your database in just few clicks.
179 stars 95 forks source link

[BUG] Programatic import returning error #138

Closed johnark88 closed 1 year ago

johnark88 commented 1 year ago

Describe the bug I am currently trying to build an import system for a singly-type page. I built the page via the content type builder and exported with this plugin. I then edited some items and am trying to import the whole page again via a service at src/api/single_type_name/services/single_type_name.js. It is importing text, wysiwyg content (just text as well) and images. Some images are in components some are not. The file I am importing is just an export from the content-manager via this plugin.

The error I get on yarn develop is

TypeError: Cannot destructure property 'slug' of 'undefined' as it is undefined.

I am going to be honest, I am not really sure what to put for slug, in the docs you specify media for media, but this is mixed content so I assumed it was the slug for the single-type. I have tried the collectionName, singularName, pluralName and displayName. Nothing works. Is this supposed to be a field in my schema? I have also tried api::join-page.join-page which did not work.

To Reproduce Here is the code I have written so far ->

module.exports = createCoreService(
  "api::join-page.join-page",
  ({ strapi }) => ({
    async importJoinPageData() {
      const service = strapi.plugin("import-export-entries").service("import");
      await service.importData({

        dataRaw:
          "../../../../database/joinPageData/export_api-join-page.join-page_230814232822932.json",
        options: {
          slug: "join_pages",
          format: "json",
          /** User importing data. */
          // user: object;
        },
      });
    },
  })
);`

it is being called in src/index.js

async bootstrap({ strapi }) {
  await strapi.service("api::join-page.join-page").importJoinPageData();
}

Again the data I am importing is an export generated via this plugin, with just text replaced/edited. It does include media files.

Node Version: 18.17.0 Strapi version: 4.12.1 (Community edition) Using yarn develop locally Database: MySQL

Expected behavior Data is imported, returns success message.

Additional context I am probably just missing something small when it comes to the slug, but appreciate any advice/help you could give. Thank you.

I have one other question. What is the user object supposed to be?

/** User importing data. */
user: object;
johnark88 commented 1 year ago

Update: I realized I was not using V2 I am now calling await service.importDataV2({}).

 await service.importDataV2({
   fileContent:  {all_my_json}, // or file import.
   options: {
     slug: "api::name.name",
     format: "json",
     user: object  // superAdmin returned from strapi.db.query.findOne({})
     idField: "name" // idField placed in api/page/content-types/pageName/schema.json  in pluginOptions:{}
 })

I am still getting the TypeError: Cannot destructure property 'slug' of 'undefined' as it is undefined. @ strapi-plugin-import-export-entries/server/services/import/import-v2.js:43:44

johnark88 commented 1 year ago

Closing. This is what I got to work -> Note: This is a single type, so idField: "" is not set, will probably need that in a collection.

    const importedJSONFILE = require('path_to_file');
    async importPageData() {
      const superAdmin = await strapi.db
        .query("admin::user")
        .findOne({ where: { username: process.env.ADMIN_NAME} });

      const service = strapi.plugin("import-export-entries").service("import");
      await service.importDataV2(importedJSONFILE, {
        slug: "api::page.page",
        format: "json",
        user: superAdmin,
      });
    },