Modify capital letters to dashed format in the ToJSON method in Nest JS

I am working with a method that looks like this:

@Entity()
export class Picklist extends BaseD2CEntity {
  @ApiHideProperty()
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ name: 'picklist_name' })
  @IsString()
  @ApiProperty({ type: String, description: 'picklist_name' })
  picklistName: string;

  toJSON() {
    return classToPlain(this);
  }

}

Currently, when I serialize the object using myPicklist.picklist.toJSON();, I get the following result:

{ id: 7, picklistName: "status", }

Everything is correct so far. However, I now need to change the capital letter in picklistName to an underscore, similar to what is specified in the @Column decorator as picklist_name. Is there a way to achieve this?

Answer №1

Customize Serialization Property Names

If you want to customize the property names during serialization, you can do so using class-transformer's @Expose decorator. Check out this helpful answer:

@Expose({ name: 'picklist_name' })
picklistName: string;

Utilize ClassSerializerInterceptor

Instead of manually creating a toJSON method, consider using the built-in ClassSerializerInterceptor for automatic entity serialization in your controller methods. See more details in this answer.

@UseInterceptors(ClassSerializerInterceptor)

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

What is causing the issue of subdomains not functioning properly in express.js?

Currently, I am conducting some local experiments and have made changes to my hosts file. Here are the entries: 127.0.0.1 example.dev 127.0.0.1 www.example.dev 127.0.0.1 api.example.dev Below is the code I am using: var subdomain = req ...

Updating the progress bar without the need to refresh the entire page is

Currently, I have two PHP pages: page.php and loader.php. Loader.php retrieves data from MySQL to populate a progress bar, while page.php contains a function that refreshes loader.php every second. This setup gets the job done, but it's not visually a ...

An easy guide to integrating cors-anywhere into your express application

Having trouble getting cors-anywhere to work with my nodejs/express setup. Initially, I attempted using express-cors-anywhere in the following way: const anywhere = require('express-cors-anywhere') app.use("/cors-anywhere", anywhere()) However, ...

Instantly change the default value with Ajax selection

My current setup involves using Ajax to populate a select HTML element with values from an array returned by PHP. Here is the Ajax code snippet: <script type = "text/javascript"> $(document).ready(function(){ $('#fds_categories&ap ...

Retrieving the input[text] value in TypeScript before trimming any special characters

One of the tasks I am working on involves a form where users can input text that may contain special characters such as \n, \t, and so on. My objective is to replace these special characters and then update the value of the input field accordingl ...

Using JavaScript to transform JSON information into Excel format

I have tried various solutions to my problem, but none seem to fit my specific requirement. Let me walk you through what I have attempted. function JSONToCSVConvertor(JSONData, ReportTitle, ShowLabel) { //If JSONData is not an object then JSON.parse will ...

"Having trouble with sound in react-native-sound while playing audio on an Android AVD? Discover the solution to fix this

react-native-sound I attempted to manually configure react-native-sound but I am unable to hear any sound. The file was loaded successfully. The audio is playing, but the volume is not audible on Android AVD with react-native-sound. ...

Error encountered when attempting to assign a value of the original data type within the Array.reduce function

I am in the process of developing a function that takes a boolean indicator object like this: const fruits = { apple: false, banana: false, orange: false, mango: false, }; Along with an array such as ['apple', 'orange']. The go ...

Find and match additional routes easily after an initial one has been discovered

Currently, I am in the process of constructing a basic blog using Express 4.13.3 and Mongoose 4.2.4, however; there is an issue that I have encountered where subsequent routes continue to match even after one route has been matched, leading to the applicat ...

Angular: Struggling with Parent Component not recognizing changes in Child Component

Currently, I am facing an issue with my shopping cart functionality. When the website first loads and a user does not have a saved shopping cart, they need to click "add to cart" before one is created. The problem lies in the fact that my app.component doe ...

Creating a personalized to-do list feature in NodeJs for exclusive user access: A step-by-step guide

After pondering over this issue for the past few days, I am still trying to grasp it. My web app allows users to login and create a list of students. My goal is to ensure that only the user who created the list can view it. Below is my progress so far. Co ...

``The background color will dynamically change according to the result of a function

My function named shift_color generates different color codes like "#FF5F74", "#5FFF66", and "#5F8AFF". I am looking to use this output to style a navigation menu background. I have tried the following code: .topnav { background-color: <?php echo shi ...

Problems with NPM Installation on Windows 7

While performing an NPM install on Windows 7, I encountered an error that I can't seem to resolve. C:\xampp\htdocs\dev4\nodejs\node_modules\jsdom\node_modules\contextify>node "C:\ Program Files (x86)& ...

Is there a way to verify the presence of a service worker on a specific URL?

Is there a way for me to determine if external websites have a 'service-worker' or not? Here is what I think could work: Extract all the JavaScript files from the given URL Look for the string 'sw.js' (I am not entirely sure how to ...

Transfer of part of a JSON object

My current setup involves an API in PHP that sends JSON data to a Javascript webpage for processing. However, when dealing with large datasets, it can strain both the user's internet connection and computer capabilities. To address this issue, I want ...

What is the best way to choose a single li element?

I am working on creating a reservation system using HTML, CSS, and JS. I want to customize the color of the border for each list item (li). However, I want only one li to be selected at a time. When I click on a different li, it should remove the selection ...

Retrieving ng-repeat $index with filtering in AngularJS controller

I am facing a challenge with my ng-repeat list and filter in AngularJS. I am unable to retrieve the visible $index value from inside my controller. Although I can display the index easily and see it change dynamically when the list is filtered, I am strug ...

Using angular2's ngRepeat to iterate over XML data

I have successfully transformed my XML data into JSON format to be utilized in Angular 2 within my view. However, as I attempt to loop through the data using a for(var... loop, I encounter an error message stating that it cannot find name array and that th ...

Tips for customizing babel's preset plugin configurations

I've integrated babel-preset-react-app into my project with the following .babelrc configuration: { "presets": ["react-app"], "plugins": [ "transform-es2015-modules-commonjs", "transform-async-generator-functions" ] } Currently, I&apos ...

{ "error": "The 'title' property of 'req.body' cannot be destructured because it is not defined." }

Here are the snippets of code for a Node.js application: // src/models/workoutsModel.js const mongoose = require("mongoose"); const Schema = mongoose.Schema; const workoutSchema = new Schema({ title: { type: String, required: true, ...