Expand my Community achievements bar.

SOLVED

Create Sling model Array

Avatar

Level 2

Hi guys, I am creating a component with variables and an array, but the array creation is not working,
generates in the database but does not generate in the template, does not generate in json.
I have 2 projects, I tested it on the other one and it worked, but not on this project, I got
I got the complete component and it still hasn't generated the information in json.
I'm using Angular to do mapTo and that's it.

ElvisRo_0-1720796365739.png

ElvisRo_1-1720796402131.png

 


import com.adobe.cq.export.json.ComponentExporter;
import com.adobe.cq.export.json.ExporterConstants;

import lombok.Getter;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Exporter;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@Getter
@Model(
adaptables = { SlingHttpServletRequest.class, Resource.class },
adapters = { CardsContainer.class, ComponentExporter.class },
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL,
resourceType = CardDustReviews.RESOURCE_TYPE
)
@Exporter(
name = ExporterConstants.SLING_MODEL_EXPORTER_NAME,
extensions = ExporterConstants.SLING_MODEL_EXTENSION
)
public class CardDustReviews implements CardsContainer {

protected static final String RESOURCE_TYPE = "projeto/components/reviews";

private static final Logger LOGGER = LoggerFactory.getLogger(CardDustReviews.class);

@ValueMapValue
private String dust;

@ChildResource
private List<CardDust> cards;


@Override
public List<CardDust> getCards() {
if (cards == null) {
LOGGER.warn("Could not locate any card. Returning an empty list.");
return Collections.emptyList();
}
return new ArrayList<CardDust>(cards);
}

@Override
public String getExportedType() {
return CardDustReviews.RESOURCE_TYPE;
}
}

CardsContainer

import com.adobe.cq.export.json.ComponentExporter;

import java.util.Collection;

public interface CardsContainer extends ComponentExporter {
String getDust();
Collection<? extends Card> getCards();
}


CardDust
import lombok.Getter;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;

@Getter
@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL,
adapters = Card.class)
public class CardDust implements Card {

@ValueMapValue
private String title;
}


public interface Card {
String getTitle();
}









1 Accepted Solution

Avatar

Correct answer by
Level 4

Hi @ElvisRo From what I understand, you have a component CardDustReviews that has a list of CardDust objects, and you're using Angular to map the data. However, the array of CardDust objects is not being generated in the JSON output.

Here are a few potential issues to consider:

  1. Check the cards list initialization: In your CardDustReviews class, you have a @ChildResource annotation on the cards list. Make sure that the cards list is properly initialized and populated with data. You can try debugging the code to see if the cards list is empty or if there's an issue with the data retrieval.
  2. Verify the getCards() method: The getCards() method returns a new ArrayList instance, but it's not clear if this is necessary. Try removing the new ArrayList<CardDust>(cards) part and simply return the cards list. Also, check if the cards list is being modified or filtered somewhere else in the code.
  3. Check the Exporter configuration: Ensure that the Exporter configuration is correct and that the CardDustReviews class is properly registered as an exporter. You can try checking the Sling logs to see if there are any errors or warnings related to the exporter configuration.
  4. Verify the Angular mapping: Double-check that the Angular mapping is correct and that the CardDustReviews component is properly mapped to the JSON output. Make sure that the Angular code is correctly referencing the cards list and that there are no typos or incorrect property names.

To help you further, could you please provide more information about the following:

  • The exact error message or behavior you're seeing when trying to access the cards list in the JSON output
  • The Angular code that's mapping the CardDustReviews component to the JSON output
  • Any relevant Sling logs or error messages that might indicate the issue

Let me know if you have any questions or if there's anything else I can help you with!

View solution in original post

7 Replies

Avatar

Employee Advisor

The @ChildResource annotation derives the name of the child resource it needs to inject from the name of the variable (or the value of the "name" parameter).
That also means, that it cannot inject more than 1 child resource, because the underlying repository does not allow sibling nodes with the same name.

Avatar

Level 2

hello,,
It worked in a dev environment, but it doesn't work locally, could it be some configuration in the dispatcher?

Avatar

Level 2

heu @Jörg_Hoh ,
It worked in a dev environment, but it doesn't work locally, could it be some configuration in the dispatcher?

Avatar

Community Advisor

Hi  @ElvisRo 
I think from what I have understood your use case is this:
1. You are using sling model delegation pattern and then you are exporting the delegated component sling model via exporter.
2. Now when you use your sling model in component directly , it works fine. All the getter methods return correct values in sightly.
3. But when you are exporting the same sling model it does not return desired json.
Please confirm if above is your issue.

For this what you need to do is specify your our json exporter in the sling model as the exporter for json that the ootb component sling model is using might not expose all methods.

Also what you can do is create and assign your own custom object again, that you want to export, in your sling model and then expose it via getter method.

Same issue I faced in delegation pattern and I used above work around for the same.

Avatar

Level 2

hello,
It worked in a dev environment, but it doesn't work locally, could it be some configuration in the dispatcher?d

Avatar

Correct answer by
Level 4

Hi @ElvisRo From what I understand, you have a component CardDustReviews that has a list of CardDust objects, and you're using Angular to map the data. However, the array of CardDust objects is not being generated in the JSON output.

Here are a few potential issues to consider:

  1. Check the cards list initialization: In your CardDustReviews class, you have a @ChildResource annotation on the cards list. Make sure that the cards list is properly initialized and populated with data. You can try debugging the code to see if the cards list is empty or if there's an issue with the data retrieval.
  2. Verify the getCards() method: The getCards() method returns a new ArrayList instance, but it's not clear if this is necessary. Try removing the new ArrayList<CardDust>(cards) part and simply return the cards list. Also, check if the cards list is being modified or filtered somewhere else in the code.
  3. Check the Exporter configuration: Ensure that the Exporter configuration is correct and that the CardDustReviews class is properly registered as an exporter. You can try checking the Sling logs to see if there are any errors or warnings related to the exporter configuration.
  4. Verify the Angular mapping: Double-check that the Angular mapping is correct and that the CardDustReviews component is properly mapped to the JSON output. Make sure that the Angular code is correctly referencing the cards list and that there are no typos or incorrect property names.

To help you further, could you please provide more information about the following:

  • The exact error message or behavior you're seeing when trying to access the cards list in the JSON output
  • The Angular code that's mapping the CardDustReviews component to the JSON output
  • Any relevant Sling logs or error messages that might indicate the issue

Let me know if you have any questions or if there's anything else I can help you with!

Avatar

Level 2

hello,
It worked in a dev environment, but it doesn't work locally, could it be some configuration in the dispatcher?