Fetching Data from node using model | Community
Skip to main content
Level 6
August 8, 2022
Solved

Fetching Data from node using model

  • August 8, 2022
  • 3 replies
  • 1117 views

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"); }

 

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Sachin_Arora_

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;
	}
 


 

3 replies

Sachin_Arora_
Community Advisor
Community Advisor
August 8, 2022

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.

Ronnie09Author
Level 6
August 8, 2022

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

Sachin_Arora_
Community Advisor
Sachin_Arora_Community AdvisorAccepted solution
Community Advisor
August 8, 2022

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;
	}
 


 

Manu_Mathew_
Community Advisor
Community Advisor
August 8, 2022

@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.

arunpatidar
Community Advisor
Community Advisor
August 9, 2022

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