Hey there! In this guide, we'll walk through different methods to add new fields to the response objects in Xano, without having to modify your actual database. This can be super handy when you need to perform calculations, combine data, or display additional information based on the existing records. Let's dive in!
Adding a Custom Field to a Single Record Response
Sometimes, you might want to add a custom field to a single record response. For instance, you have a few fields in a record that you want to calculate and display the total within the same response object. Here's how you can do it:
- Create a Variable: First, create a variable to store the calculated value. For example, let's call it `total`.
- Set Variable Value: In the variable value, perform the calculation you need. Let's say your database table has a `cost` field and an `amount` field. You can multiply them like this: `items.cost * items.amount`.
- Add Variable to Response: Now, you can simply add this `total` variable to the response. However, it will be separate from the actual record object.
- Update Variable to Add Field: To include the custom field within the record object, go to the "Update Variable" step. Here, you can use dot notation to create a new field. Type the variable name you want to update (e.g., `item`), followed by a dot and the new field name (e.g., `result`). For the value, enter the calculation you want (e.g., `total`).
- Run and Enjoy!: When you run the query, you'll see the `result` field added to the record object with the calculated value.
Adding Custom Fields to Multiple Record Responses
What if you want to add a custom field to multiple records in a response? There are a couple of methods you can use:
Method 1: Using a Loop
- Create a Loop: When querying multiple records, you'll need to use a loop to manipulate each record individually. Create a `for each` loop and give it a variable name (e.g., `loop`).
- Update Variable Inside Loop: Inside the loop, update the variable with the new field and calculation, just like in the single record example. Use the loop variable (e.g., `loop.cost`, `loop.amount`) to reference the record fields.
- Run and Enjoy!: The custom field will be added to each record object in the response.
Method 2: Using Evals (Recommended for Multiple Records)
Xano's `evals` feature allows you to reference fields in your query without using an add-on. This can be a more efficient method for adding custom fields to multiple records:
- Navigate to Evals: In the "Output" step, scroll down to the "Evals" section.
- Add a New Eval: Click "Add Eval" and give it a name (e.g., `total`).
- Reference Fields and Calculate: Use dot notation to reference the fields from your query (e.g., `items.cost`). Apply any filters or calculations you need, like multiplying by `items.amount`.
- Run and Enjoy!: The new field with the calculated value will be included in each record object in the response.
And there you have it! These methods allow you to add custom fields to your responses without modifying the actual database. This can be incredibly useful for displaying calculated values, combining data, or presenting information in a specific way, all without changing your data model.
Give it a try and let me know if you have any questions! Happy no-code building with Xano!