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