Problems creating an array of structures

I am creating a blog API and I am having some very strange problems trying to create an array of structures in coldfusion. The top-level array will contain a post as a structure, with .comments, which is an array of all the comments in this post, as well as structures.

Each of the parts in the following code works individually. But somehow, when I add them, I end up with an infinitely nested array of structures containing an array of structures, etc ... just the very last element in the top-level message array.

<cfset posts = VARIABLES.postDao.getBlogPosts(argumentCollection=arguments) /> <cfset result = arraynew(1) /> <cfloop index="i" from="1" to="#arrayLen(posts)#"> <cfset post = posts[i].getInstance()> <cfset StructInsert(post, 'comments', getComments(post.postId))> <cfset ArrayAppend(result, post)> </cfloop> 

getBlogPosts returns an array of beans messages.
bean.getInstance () returns a structure with all the data in the bean.
getComments (id) returns an array of all comments (structs) for post [id].

Each of them works as intended and is used in other places without problems.

The structure of an infinitely nested array is as follows:

 Array containing Post . Post.comments containing array of comments + Post on end . . Post.comments containing array of comments + Post on end . . . etc... 
+4
source share
2 answers

You have not provided all the code.

I suspect that you have replaced what you showed, or one of them will solve the problem:

 <cfset local.posts = VARIABLES.postDao.getBlogPosts(argumentCollection=arguments) /> <cfset local.result = arraynew(1) /> <cfloop index="local.i" from="1" to="#arrayLen(local.posts)#"> <cfset local.post = local.posts[local.i].getInstance()> <cfset StructInsert(local.post, 'comments', getComments(local.post.postId))> <cfset ArrayAppend(local.result, local.post)> </cfloop> 


Or:

 <cfset var posts = VARIABLES.postDao.getBlogPosts(argumentCollection=arguments) /> <cfset var result = arraynew(1) /> <cfset var i = 0 /> <cfset var post = 0 /> <cfloop index="i" from="1" to="#arrayLen(posts)#"> <cfset post = posts[i].getInstance()> <cfset StructInsert(post, 'comments', getComments(post.postId))> <cfset ArrayAppend(result, post)> </cfloop> 


You should always use the var keyword or local scope for variables in cffunction.

You can use VarScoper to check your code for other places where it is necessary for correction.

+7
source

Try adding some cfdumps there and tell us what happened:

 <cfset posts = VARIABLES.postDao.getBlogPosts(argumentCollection=arguments) /> <cfset result = arraynew(1) /> <cfloop index="i" from="1" to="#arrayLen(posts)#"> <cfset post = posts[i].getInstance()> <cfdump var="#post#"> <cfset StructInsert(post, 'comments', getComments(post.postId))> <cfdump var="#post#"> <cfset ArrayAppend(result, post)> <cfdump var="#result#"><cfabort> </cfloop> 

change

I think the problem is with a child reference to the parent value, which spawns an infinite loop when recursing through an object. Try changing this:

 <cfset posts = VARIABLES.postDao.getBlogPosts(argumentCollection=arguments) /> <cfset result = arraynew(1) /> <cfloop index="i" from="1" to="#arrayLen(posts)#"> <cfset post = posts[i].getInstance()> <cfset StructInsert(post, 'comments', Duplicate(getComments(post.postId)))> <cfset ArrayAppend(result, post)> </cfloop> 
0
source

Source: https://habr.com/ru/post/1381350/


All Articles