It was causing errors for anonymous (logged out) users, this adds an additional check to avoid the error cause.