skip to Main Content

I have an API call I am making:

function createReferral(referralData: { actionTypeId: number }) {
  referralAxios.post(`/api/ReferralMasters`, {
    appTypeID: referralData.appTypeId,
  }).then(function (response) {
    console.log(actionTypeId)
  })
}

I am getting an error message saying that actionTypeId is not defined.

How do I go about passing my function parameter to the .then part of the API call?

3

Answers


  1. The attribute actionTypeId is a part of the referralData object and therefore not visible to the then function. Try to print it the following way instead:

    .then(function (response) {
      console.log(response.data.actionTypeId);
    })
    
    Login or Signup to reply.
  2. You have to get that value from referralData:

    function createReferral(referralData: { appTypeId: number, actionTypeId: number }) {
      referralAxios.post(`/api/ReferralMasters`, {
        appTypeID: referralData.appTypeId,
      }).then(function (response) {
        console.log(referralData.actionTypeId)
      })
    }
     
    createReferral({ appTypeId: 10, actionTypeId: 20 })
    
    Login or Signup to reply.
  3. Your parameter description does not desctructure actionTypeId from parameters.
    If you would like to destructure, this is the way to do it.

     function createReferral({ actionTypeId, appTypeId }: {actionTypeId: number, appTypeId: number}) {
        referralAxios.post(`/api/ReferralMasters`, {
          appTypeID: appTypeId,
    
        })
          .then(function (response) {
            console.log(actionTypeId)
          })
      }
    

    However, if you would like to simplify, it’s best not to destructure, and use interfaces/types for type definition.

    interface ReferralInterface {
      actionTypeId: number;
      appTypeId: number;
    }
    
    function createReferral(referralData: ReferralInterface) {
       referralAxios.post(`/api/ReferralMasters`, {
         appTypeID: referralData.appTypeId,
    
       })
         .then(function (response) {
           console.log(referralData.actionTypeId)
         })
     }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search