0

I am trying to use an if/else and I can't figure out where I'm going wrong with the syntax. I want the payload to only submit school_ids if the role isn't p_admin. Thank you for your help!

const createUser = (payload) => request.postJSON(
    '/register',
    excludeObjNullValue({
        username: payload.username,
        email: payload.email,
        password: payload.password,
        first_name: payload.firstName,
        last_name: payload.lastName,
        role: payload.role,
        district_id: payload.districtId,
        if (role !== 'p_admin'){
        school_ids: payload.schoolIds
        }
    })
);

2 Answers 2

4

I see 2-3 options depending on your excludeObjNullValue function:

  1. You can either break that expression up in to a couple of statements, or

  2. Or use property spread notation and the conditional operator, or

  3. If excludeObjNullValue does what the name suggests, you can use a conditional without spread notation and rely on excludeObjNullValue to exclude it

Option 3 may well be your best bet, but I'll give them in order:

Option 1: The multi-statement way:

const createUser = (payload) => {
    const options = {
        username: payload.username,
        email: payload.email,
        password: payload.password,
        first_name: payload.firstName,
        last_name: payload.lastName,
        role: payload.role,
        district_id: payload.districtId,
    };
    if (role !== 'p_admin') {
        options.school_ids = payload.schoolIds;
    }
    return request.postJSON('/register', excludeObjNullValue(options));
};

Option 2: Property spread and the conditional operator

Property spread is a Stage 3 proposal but support for it is already shipping in Chrome and Firefox, and if you're transpiling you can tell your transpiler to support it. That would look like this:

const createUser = (payload) => request.postJSON(
    '/register',
    excludeObjNullValue({
        username: payload.username,
        email: payload.email,
        password: payload.password,
        first_name: payload.firstName,
        last_name: payload.lastName,
        role: payload.role,
        district_id: payload.districtId,
        {...(role !== 'p_admin' ? {school_ids: payload.schoolIds} : {}}
        }
    })
);

Whether it's a good idea even if supported is your call. :-) It does briefly create an extra object and iterator.

Option 3: Rely on excludeObjNullValue to exclude properties with null values:

If it really does what the name suggests, then it will remove school_ids if we give the value null to it. So:

const createUser = (payload) => request.postJSON(
    '/register',
    excludeObjNullValue({
        username: payload.username,
        email: payload.email,
        password: payload.password,
        first_name: payload.firstName,
        last_name: payload.lastName,
        role: payload.role,
        district_id: payload.districtId,
        school_ids: role !== 'p_admin' ? payload.schoolIds : null
        }
    })
);
Sign up to request clarification or add additional context in comments.

5 Comments

(If you see -- or rather don't see -- a missing ) in the first code block at the end, hit refresh.)
In JS, is an inline expression valid? i.e., school_ids: payload.role !== 'p_admin' ? payload.schoolIds : ""
@MetroSmurf: That would still submit school_ids, it would just submit it either with the value payload.schoolIds or "".
Guess I didn't read the question correctly, re: only include the property when the condition exists. As opposed to include the property with a value or some default value.
@MetroSmurf: That said, they are passing that object into a function called excludeObjNullValue, which if it does what the name suggests, means we could use school_ids: role !== 'p_admin' ? payload.schoolIds : null and rely on excludeObjNullValue to exclude the property if its value is null. :-) I added it.
1

You can't write if statement at the object declaration. Also you need to explicitly defined body for your arrow function. So you can do something like

const createUser = (payload) => {
   const obj = {
      username: payload.username,
        email: payload.email,
        password: payload.password,
        first_name: payload.firstName,
        last_name: payload.lastName,
        role: payload.role,
        district_id: payload.districtId
   };

   if(obj.role !== 'p_admin') {
      obj.school_ids = payload.schoolIds
   }

   return request.postJSON('/register', excludeObjNullValue(obj));

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.