SM1105922
SM1105922

Reputation: 127

Not able to pass parameter to function

I am trying to pass the value in the state as a parameter to the Function.addAddOnData to which calls the setaddOndatatypes function with " addOnCategory" as a parameter, the parameter is not getting passed on doing console.log it shows blank as the response.

First function call:

  const onSubmit = e => {
    e.preventDefault();
    fileUpload(fileInputRef);
    if (!state.loading) {
      addAddOnDataToDb(
        state.addOnCategory,
        state.itemName,
        state.itemPrice,
        state.itemIconURL
      );
    }
  };

Function:

const addAddOnDataToDb = (
    itemName,
    itemIconURL,
    itemPrice,
    addOnCategory
  ) => {
    console.log(addOnCategory);

    const addOnType = setAddOnItemType(addOnCategory);
    console.log(addOnType);
    const addOnBody = JSON.stringify({
      itemName,
      itemIconURL,
      itemPrice,
      addOnType
    });
    console.log(addOnBody);
    const config = {
      headers: {
        'Content-Type': 'application/JSON'
      }
    };
    axios.post('/api/v1/addOn', addOnBody, config);
  };

Call to setAddOnItemType

  const setAddOnItemType = addOnCategory => {
    console.log(addOnCategory);
    switch (addOnCategory) {
      case 'Add on':
        return 'addOn';
      case 'Flavours':
        return 'flavour';
    }
  };

Upvotes: 0

Views: 297

Answers (1)

MjZac
MjZac

Reputation: 3526

You have wrong ordering of arguments at the addAddOnDataToDb call side.

you defined the function like:

const addAddOnDataToDb = (
    itemName,
    itemIconURL,
    itemPrice,
    addOnCategory
  ) => {
    ....
  }

And your call looks like:

addAddOnDataToDb(
    state.addOnCategory,
    state.itemName,
    state.itemPrice,
    state.itemIconURL
  );

state.addOnCategory should be passed as the last argument to the function call. Also the price and icon url are also in wrong order.

addAddOnDataToDb(
    state.itemName,
    state.itemIconURL,
    state.itemPrice,
    state.addOnCategory,
  );

Upvotes: 1

Related Questions