Giter VIP home page Giter VIP logo

Comments (8)

ranaharoon3222 avatar ranaharoon3222 commented on June 29, 2024 1

thank you soo much you are great!!!!!

from ra-strapi-rest.

nazirov91 avatar nazirov91 commented on June 29, 2024

Do you have any suggestions on how to account for this type of situations without breaking other scenarios (i.e. image upload) ? A pull request will be appreciated. Thank you!

from ra-strapi-rest.

AsharDweedar avatar AsharDweedar commented on June 29, 2024

Actually no I don't, the only solution I could do is this if condition; other solution might be adding a custom endpoint on strapi to get the component's document.

I wished you knew a better way than mine; nevertheless; not sure if you would consider the approach of receiving a list of "KEY_IN_DOCUMENT_REFERS_TO_AN_ARRAY_OF_COMPONENTS" like the one I used when initialing the data provider alongside the API link; that I can do a pull request for.. feels like a silly way to go but that can only other way be handled this way; or by customizing strapi to not send id in the component's object or sending "isComponent" key with it to use it as a reference

from ra-strapi-rest.

ranaharoon3222 avatar ranaharoon3222 commented on June 29, 2024

where to add above code?

from ra-strapi-rest.

AsharDweedar avatar AsharDweedar commented on June 29, 2024

@ranaharoon3222
Fnd the function replaceRefObjectsWithIds, this is the one responsible for replacing the json[key] with either the object's ID or array of IDs if the value was an array of components; there you should add your condition to not replace the value in the document if it was a component.

for me my code looked like this:

  // Replace reference objects with reference object IDs
  const replaceRefObjectsWithIds = (json) => {
    Object.keys(json).forEach((key) => {
      const fd = json[key]; // field data
      const referenceKeys = [];
      if (fd && (fd.id || fd._id) && !fd.mime) {
        if (!isComponentsKeys(key)) {
          json[key] = fd.id || fd._id;
        } else {
          delete fd.id;
        }
      } else if (Array.isArray(fd) && fd.length > 0 && !fd[0].mime) {
        fd.map((item) => referenceKeys.push(item.id || item._id));
        if (!isComponentsKeys(key)) {
          json[key] = referenceKeys;
        }
      }
    });
    return json;
  };

isComponentsKeys is a function I defined to decide whether this is a component object or a populated object id for a document, you can add your logic there.

from ra-strapi-rest.

ranaharoon3222 avatar ranaharoon3222 commented on June 29, 2024

can you please share isComponentsKeys function please

from ra-strapi-rest.

AsharDweedar avatar AsharDweedar commented on June 29, 2024

mine is as simple as:

const componentsKeys = [
  "key1",
  "key2"
];

function isComponentsKeys(k) {
  return componentsKeys.indexOf(k) !== -1;
}

where I have documents like:

[
  {
    "id": "---",
    "name:": "something",
    "key1": {
      "id": "component id",
      "option": "some value"
    },
    "key2": [
      {
        "id": "component 2 id",
        "option": "some value"
      },
      {
        "id": "component 3 id",
        "option": "some other value"
      },
    ]
  }
]

it's stupidly hard coded so you might have a better way.

from ra-strapi-rest.

nazirov91 avatar nazirov91 commented on June 29, 2024

Hi!

Please see the readme file. I have updated the package to make it work with Strapi V4. Please open a new issue if the problem persists.

Thanks!

from ra-strapi-rest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.