I'm just trying to create a basic recursive function that returns a result once it reaches a certain number. However I keep getting this error. Anyone have any ideas whats wrong here?
Here is my code:
GLOBAL = {};
GLOBAL.cnt = 0;
Parse.Cloud.define('recursiveTest', function(request, response)
{
GLOBAL.cnt++;
if(GLOBAL.cnt >= 2) {
response.success(GLOBAL.cnt);
}
request = {};
Parse.Cloud.run('recursiveTest', request, response);
});
Here is the full error:
{"code":141,"error":"{\\"code\\":141,\\"message\\":\\"{\\\\"code\\\\":141,\\\\"message\\\\":\\\\"{\\\\\\\\"code\\\\\\\\":141,\\\\\\\\"message\\\\\\\\":\\\\\\\\"{\\\\\\\\\\\\\\\\"code\\\\\\\\\\\\\\\\":141,\\\\\\\\\\\\\\\\"message\\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\\"{\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"code\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\":124,\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"message\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"Too many recursive calls into Cloud Code\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"}\\\\\\\\\\\\\\\\"}\\\\\\\\"}\\\\"}\\"}"}
It looks like your function is infinitely recursive. Maybe you wanted to do this?
Parse.Cloud.define('recursiveTest', function(request, response)
{
GLOBAL.cnt++;
if(GLOBAL.cnt >= 2) {
// added `return` here
return response.success(GLOBAL.cnt);
}
request = {};
// added `return` here
return Parse.Cloud.run('recursiveTest', request, response);
});
In any case, you have to return
to terminate the recursion at some point, since otherwise the function will run forever (or practically until you get a too much recursion error) without ever delivering the desired result.
I figured out somewhat of a workaround. It works in my situation at least:
Parse.Cloud.define('recursiveTest', function(request, response)
{
function doRecursive(cnt)
{
cnt++;
if(cnt >= 2) {
response.success(cnt);
}
}
doRecursive(0);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.