Expand my Community achievements bar.

SOLVED

Fetching Data from node using model

Avatar

Level 7

I have one component(Name, Price, Button Label) which has button . Whenever button is clicked it should redirect to new page and fetch the same data and print there.

 

I am trying to use href with parameters p and name using which I coul get the data and show in new component present there. I have written below code which is giving error org.apache.sling.scripting.sightly.render.ObjectModel Cannot access method name on object com.sample.core.models.DescriptionModel@76c56976

Tried restarting aem, refreshing bundle giving build etc nothing worked

 

@Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class DescriptionModel {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @SlingObject
    SlingHttpServletRequest request;
    
    @OSGiService
    private transient ResourceResolverFactory resolverFactory;
    
    public ValueMap getResource() throws LoginException {
        ResourceResolver resolver=request. getResourceResolver();
        Resource resource = resolver.getResource(request.getParameter("p"));
        logger.info("RESOURCE IS {}",resource.getChild("jcr:content/"+request.getParameter("name")).getPath());
        return resource.getChild("jcr:content/"+request.getParameter("name")).getValueMap();

    }



    public String getName() throws LoginException {
        ValueMap properties=getResource();
        return (String) properties.get("name");
    }

    public int getPrice() throws LoginException {
        ValueMap properties=getResource();
        return (int) properties.get("price");
    }

    public String getBtnLabel() throws LoginException {
        ValueMap properties=getResource();
        return (String) properties.get("btnLabel");
    }

   

 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Please create @PostConstruct and move logic of getResource() to @PostConstruct method. Just have a private variable which can be initialized in Post Construct method and passed to getName().

Basically if any exception comes in getter you get this issue. You can go to bottom of stack trace to get exact exception.

Refer below dummy code which will fix your error :

public String getName() throws LoginException {
		String dummyString = "dummy";
		try {
			ValueMap properties = getResource();
			dummyString = (String) properties.get("name");
		}catch(Exception e) {
			//TODO
		}
		return dummyString;
	}
 


 

View solution in original post

5 Replies

Avatar

Community Advisor

Please check if there is any error when bundle is installed and check the timestamp of the Last Modification Time of your bundle in Felix console. Probably the bundle with latest changes is not getting installed.

Also you can follow the best practice by creating an interface and implementation class for Model. Moving getResource() in @PostConstruct will execute getting ValueMap only once.

Avatar

Level 7

Bundle is updating properly and I will follow best practice for debugging only I did that still not working

Avatar

Correct answer by
Community Advisor

Please create @PostConstruct and move logic of getResource() to @PostConstruct method. Just have a private variable which can be initialized in Post Construct method and passed to getName().

Basically if any exception comes in getter you get this issue. You can go to bottom of stack trace to get exact exception.

Refer below dummy code which will fix your error :

public String getName() throws LoginException {
		String dummyString = "dummy";
		try {
			ValueMap properties = getResource();
			dummyString = (String) properties.get("name");
		}catch(Exception e) {
			//TODO
		}
		return dummyString;
	}
 


 

Avatar

Community Advisor

@Ronnie09 May be you can try creating a private String variable and assign the value of your valuemap to that, write a getter for that variable and use it.

Avatar

Community Advisor

Hi,

Don't do it this at client side because if the page is cached then you will have the same value for all the users.

better to fetch value using a servlet which can be called upon the click or next page load.



Arun Patidar