Steps for transforming a payload into formData:

Is there a method in Angular that allows conversion of a JSON payload object into FormData? Here is an example code snippet:

#Payload Output

{
    "id": 0,
    "name": null,
    "dealType": "Partner Location Submission",
    "annualRentProposed": null,
    "annualRentCurrent": null,
    "firmTermRemaining": null,
    "firmTermAdded": null,
    "maxAvailableTerm": null,
    "status": null,
    "capitalContribution": null,
    "parentCloneId": null,
    "accountId": 4,
    "transactionId": 173,
    "dealTypeValues": "{"id":0,"summary":"13123","mlasId":2,"startDate":"2021-10-27","endDate":"2021-10-27","rent":"2321","cam":"312","securityMonitoringMonthly":"12312","supportServicesFee":"312","estimatedOtherRevenue":"12312","descriptionOfOtherRevenue":"3123123","totalMonthlyRentAndFees":15380,"buildOutCostReimbursement":"123123","dealId":0,"startDateString":"2021-10-27","endDateString":"2021-10-27"}",
    "isReadyForApproval": false
}

#Payload Code

const payload = {
        "id": 0,
        "name": this.dealPLSFormFields.dealName,
        "dealType": "Partner Location Submission",
        "annualRentProposed": null,
        "annualRentCurrent": null,
        "firmTermRemaining": null,
        "firmTermAdded": null,
        "maxAvailableTerm": null,
        "status": null,
        "capitalContribution": null,
        "parentCloneId": null,
        "accountId": this.currentAccount.accountId,
        "transactionId": this.transactionData.id,
        "dealTypeValues": JSON.stringify(dealTypeValues),
        "isReadyForApproval": this.isReadyForApproval    
      }

Answer №1

If you need to convert an object into FormData, you can achieve this by mapping the keys of the object:

const myObject = {x: 100, y: 200};

const formData = new FormData();

Object.keys(myObject).forEach(property => {
    formData.append(property, myObject[property]);
});


Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

One may encounter the issue of 'result?.expand' potentially being 'undefined', even if an optional variable flag is provided

Currently, I am working on a small project using PocketBase and Svelte. However, in vscode, I encountered syntax highlighting errors with this particular code snippet. After researching other discussions, it was recommended that using result?.expand should ...

Implementing a class addition on focus event using Angular 2

Currently, I am in the process of upgrading an Angular 1 application to Angular 2 and encountering an issue with one of my existing directives. The task at hand is straightforward. When an input field is focused, a class should be added (md-input-focus) a ...

Guide to verify if two distinct child dropdowns share the same value using Angular 7

I have implemented two dropdowns as child components with the same data inside. My goal is to show a mat-error message if the selected values in both dropdowns are the same. I am using a function named 'areValuesTheSame()' for this purpose: are ...

Error Message: Angular 5 - Unable to bind to 'ngModel' as it is not recognized as a valid property of 'input'

I am currently working on an Angular 5 online tutorial using Visual Studio Code and have the following versions: Angular CLI: 7.0.6 Node: 10.7.0 Angular: 7.0.4, Despite not encountering any errors in Visual Studio Code, I am receiving an error in ...

Exclude all .js files from subdirectories in SVN

In my typescript project, I am looking to exclude all generated JavaScript files in a specific folder from SVN. Is there a convenient command or method to achieve this for all files within the directory? ...

The issue arises when TypeScript declarations contain conflicting variables across multiple dependencies

My current project uses .NET Core and ReactJS. Recently, I updated some packages to incorporate a new component in a .tsx file. Specifically, the version of @material-ui/core was updated from "@material-ui/core": "^3.0.3" to "@material-ui/core": "^4.1.3" i ...

Ways to enhance the type definitions for a built-in HTML element in Vue.js?

Imagine having an element that wraps around an input and inherits all of its properties, along with some extras. In React, you would define this as: interface ExtendedInputProps extends React.ComponentPropsWithoutRef<'input'> { some: T ...

Exploring the process of associating a string with a specific enum value in TypeScript

One scenario is if you receive a string value from the server and have an enum type with string values defined. In TypeScript, how can you convert the string value to the enum type? export enum ToolType { ORA= 'orange', ST= 'stone' , ...

Exporting key/value objects with React components as values in Typescript

I have a .tsx file where I need to export an object containing key/value pairs. Each value is going to be a React component used in another file. While I will have multiple key/value pairs, I'm focusing on just one at the moment. object.tsx import { ...

You are able to use a null type as an index in angular.ts(2538) error message occurred

onClick() { let obj = { fName: "ali", LName: "sarabi", age: "19", } let fieldName = prompt("field"); alert(obj[fieldName]); } I encountered an issue with the code above where alert(obj[fieldName] ...

Access to the 'currentUrlTree' property is restricted to the 'Router' class as it is marked as private and cannot be accessed externally

Currently, I am in the process of developing a login feature using jwt. However, I am encountering an error when attempting to retrieve the current URL of the active route as shown below. Error Message: [default] /Users/~/src/app/app.component.ts:51:75 P ...

Guide to displaying an input box depending on the selection made in a Mat-Select component

I am working on a mat-select component that offers two options: Individual Customers and Organizational Customers. When selecting Individual Customers, the dropdown should display three options: First Name, Last Name, and Customer Id. However, when choosin ...

Struggling to pass a method header to event handling in React markup

Within my render() method, I have a component with event handling capabilities. render() { ... <input type="text" onChange={(_) => { this.renew("email", _.target.value); }} /> } private renew(type: string, input: any) { ... if (typ ...

VS Code couldn't locate a definition for the function 'myMethod'

For some reason, I am unable to find a definition for 'myMethod' in VS Code. In my angular projects, after importing one project into VS Code, I can easily navigate to definitions using the 'F12' key. However, when I import another pro ...

Issues with Angular route links not functioning correctly when using an Array of objects

After hard coding some routerLinks into my application and witnessing smooth functionality, I decided to explore a different approach: View: <ul class="list navbar-nav"></ul> Ts.file public links = [ { name: "Home&quo ...

At times, MomentJS may miscalculate and add an incorrect number of hours

My goal is to add a specific amount of hours to a 24-hour time, but I'm encountering an issue with the time 00:00. The code I've written works correctly for all times except midnight. For example, if I have 01:30 and add 1 hour, it gives me 02:3 ...

Having trouble getting the onClick function to work in your Next.js/React component?

Recently, I delved into using next-auth for the first time and encountered an issue where my login and logout buttons' onClick functions stopped working when I resumed work on my project the next day. Strangely, nothing is being logged to the console. ...

Unable to locate the _app.js file within my Next.js project

After using "npx create-next-app" to set up my NextJs project, I came across a situation where I needed to define TransactionContext in _app.js. However, I encountered the issue that this file is not included in my project. Any assistance would be greatly ...

Setting up SonarQube for an AngularJS project utilizing Typescript is a breeze with these simple steps!

After setting up SonarQube and SonarScanner and successfully scanning my project, I noticed that all metrics appear on my screen except for code coverage, which remains at 0%. What could be causing this issue? #----- Default SonarQube server sonar.host.url ...

Error Message in Terminal When Launching React Application Using Webpack

I am encountering several errors in the node terminal while attempting to launch my react-app [at-loader] ./node_modules/@types/webpack/index.d.ts:23:16 TS2665: The module name in augmentation is invalid. Module 'webpack/lib/dependencies/HarmonyE ...