Search code examples
javascriptreactjsreact-querymutation

The argument passed inside a function becomes undefined


 const onCellClick = p => {
    const policyId = p?.id;
    const updatedBy = p?.row?.updated_by;
    console.log(updatedBy, "updatedBy");
    applyPolicies(policyId, updatedBy);
  };

This is the another function where I am doing an API call with pID and updatedBy, the updatedBy becomes undefined where pID is fine.

export const useApplyPolicyMutation = () => {
  return useMutation(
    (pID, updatedBy) => {
      console.log(updatedBy, "updatedByinquery");
      console.log(pID, "pID");
      const workspaceId = getWS();
      const payload = {
        workspace_id: workspaceId,
        policy_id: pID,
        updated_by: updatedBy
      };
      customPost("/policymanagement/api/v1/auto-discover/add-policy", payload);
    }

Solution

  • useMutation can only take one argument as payload, so you need to wrap it in an object:

    export const useApplyPolicyMutation = () => {
      return useMutation(
    -    (pID, updatedBy) => {
    +    ({ pID, updatedBy }) => {
    
    - applyPolicies(policyId, updatedBy);
    + applyPolicies({ pID: policyId, updatedBy });