Home > Back-end >  Pass request model inside of a parameter
Pass request model inside of a parameter

Time:12-12

I am trying to pass a model that I send to an API inside on a parameter but get an error stating:

Type 'string' is not assignable to type 'AVAFModel'.

submitAvafDetails(): Observable<any> {
  this.avafBcmsVo = 'avafVO:{'   this.prepareAvafSubmitData()  '}';
  return this.httpService.processData('POST', CONSTANTS.API_URL   'testService.exp', this.avafBcmsVo);
}

The error happens on this line:

this.avafBcmsVo = 'avafVO:{'   this.prepareAvafSubmitData()  '}';

Here is my model:

export interface AVAFModel {
  bankingDetails: BankingDetails;
  digitalAcquisitionsConsent: DigitalAcquisitionsConsent;
  employmentDetails: EmploymentDetails;
  financeDetails: FinanceDetails;
  incomeAndExpenseDetails: IncomeAndExpenseDetails;
  personalAddressDetails: PersonalAddressDetails;
  personalDetails: PersonalDetails;
  vehicleAssetDetails: VehicleAssetDetails;
  preQualifiedCustomer: boolean | true;
}

prepareAvafSubmitData() {
  const avafBcmsRequestObj: AVAFModel = {
    bankingDetails: bankingDetailsVoModel,
    digitalAcquisitionsConsent: digitalAcquisitionsConsentVoModel,
    employmentDetails: employmentDetailsVoModel,
    financeDetails: financeDetailsVoModel,
    incomeAndExpenseDetails: incomeAndExpenseDetailsVoModel,
    personalAddressDetails: personalAddressDetailsVoModel,
    personalDetails: personalDetailsVoModel,
    vehicleAssetDetails: vehicleAssetDetailsVoModel,
    preQualifiedCustomer: this.getPrequalifiedStatus
  };

  return avafBcmsRequestObj;
}

CodePudding user response:

  1. You should declare another object variable instead of reusing this.avafBcmsVo which leads to an unmatched type.

  2. Don't build JSON string manually. This will highly chance to lead to an invalid JSON due to syntax error. Instead, converting an object to JSON string via JSON.stringify().

submitAvafDetails(): Observable<any> {
  let avafBcmsVo: any = {
    avafVO: this.prepareAvafSubmitData()
  };

  return this.httpService.processData('POST', CONSTANTS.API_URL   'testService.exp', JSON.stringify(avafBcmsVo));
}
  • Related