Question

I have the following webscript ( disable-affiliate.put.js )

var spaceNode = customSpacesService.getAffiliatedSpace(args.id) ;

if(spaceNode == null){
    status.code = 404;
    status.message = "Can't find affiliated";
    status.redirect = true;
}

spaceNode.addAspect ("cnrps:replaceable");
spaceNode.properties["cnrps:motifReplace"] = args.motif;
var affNonValides = customSpacesService.getEntrepotsDesAffiliesSupprimesErrones();
spaceNode.move(affNonValides);

The problem is when the spaceNode is null, a status template is supposed to be returned as it always do, but an exception is thrown saying Cannot call method "addAspect" of null

So it is obvious that the redirection didn't happen and the script execution go on, Any help please...

Was it helpful?

Solution

The web scripts engine will execute your script, and afterwards will check the status variable to understand what to do with the original request. This means that the execution flow of your Javascript will not end at the line

status.redirect = true;

if you look at it, it's really just an assignment and not a method call. Put the code that uses spaceNode in an else branch and you're good to go:

var spaceNode = customSpacesService.getAffiliatedSpace(args.id) ;

if(spaceNode == null){
    status.code = 404;
    status.message = "Can't find affiliated";
    status.redirect = true;
}
else {
    spaceNode.addAspect ("cnrps:replaceable");
    spaceNode.properties["cnrps:motifReplace"] = args.motif;
    var affNonValides = customSpacesService.getEntrepotsDesAffiliesSupprimesErrones();
    spaceNode.move(affNonValides);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top