Utilize Express to Refine HTTP Responses

I am currently in the process of developing a polls application using express. The structure of my data is as follows:

var responseSchema = new mongoose.Schema({
    responseText: String,
    voters: []

})
module.exports = mongoose.model('Responses', responseSchema);

var pollsSchema = new mongoose.Schema({
    question: String,
    responses: [responseSchema],
})

When handling routes, I extract the ID from the parameters and generate an object that contains relevant information for the specific poll. However, this object includes a 'voters' array which stores SessionID's of users who have voted for each response. It is not ideal for this array to be included in the response.

My goal is to send back all details except for the 'voters' array. Is there a way to filter the http response so that it only contains the necessary data?

Answer №1

If you have a data structure containing an array called voters and you wish to eliminate this array, you can achieve this by using delete obj.voters to remove the property from the object.

For example, if your object looks like this:

var obj = {
    someField: someValue,
    someOtherField: someOtherValue,
    voters: [...]
}

You can delete the voters property like so:

delete obj.voters;

Just a heads up, if you are converting this database result into JSON for an Express route, another method would be to build a new object specifically for the HTTP response and only include the properties you want from the database query result object. This approach involves creating a deliberate representation of the response object instead of simply mirroring what is in your database. By doing this, any additions to the database fields will not automatically appear in the JSON returned from your route unless you intentionally update your route. Essentially, the route will only return the fields that you chose to include, rather than everything in the database query result by default.

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

Parse JSON data, iterate through dictionaries, and convert into markup using Python

I'm working with a JSON file that contains information about different people: { "PersonA": { "Name": "Woman A", "Age": 23, "Info": "Likes cats ..." }, "PersonB": { "Name": "Man B", "Age": 32, ...

Create an interactive list with the ability to be edited using HTML and

I'm currently working on a UI scenario that involves a text box field with two buttons beneath it. When the user clicks the first button, a popup will appear prompting them to input an IP address. Upon submitting the IP address in the popup, it will b ...

Quasar Troubles with Touch Swipe Gestures

I'm facing two issues, the first being that the directives are not functioning as expected. For example, I've implemented a swipe only to the right: <div class="q-pa-md row justify-center"> <q-card v-touch-swipe. ...

What is the process for isolating characters from a variable?

Is there a way to extract the first character up to 1 character after the decimal point? I am receiving data from an API {POWER : 1343.45} {POWER : 15623.34577} {POWER : 123.434} I only require 123.4 from 123.45, 15623.3 from 15623.34577, etc. How can I ...

What is the best way to notify an XML file using jQuery?

I am encountering an issue with a login api when trying to send an xml document. The error occurs when including '<?', but I need to send the whole XML with it. Can someone assist me in sending the complete XML using a different method or type ...

Error: The value "<html>" is of type java.lang.String and cannot be converted to a JSONObject

I am currently working on creating a login interface for an Android application. I need to establish a connection to a PHP file that connects to a database named "daymanager" with a table called "login". Below is the content of the PHP file named login.ph ...

Receiving Output from an AJAX Request

My PHP script is set up to verify the validity of an email address, returning either true or false. I've been attempting to use AJAX to call this PHP file and pass the result back to a Javascript function, but unfortunately, I haven't been succes ...

Modifying all occurrences of a specified string in an object (or array) - JavaScript

Is there a more efficient way to search through and replace all instances of a given string in a JavaScript object with unknown depth and properties? Check out this method, but is it the most optimal solution? var obj = { 'a' : 'The foo ...

Ensure that the elements within the container automatically adjust their height to fill the entirety of the

I am facing a challenge in getting the items within a container to occupy the space in the desired way. The main issue lies in the fact that flexbox and CSS grid are designed to adjust to changes in width rather than height, whereas I require the opposite ...

Utilizing Ajax POST for Jquery/Php to display real-time processing updates

I am in the process of developing a webpage for Excel data processing. At the moment, I have an index page where I submit JSON data (such as filename, selected columns, worksheet, etc.) using $.ajax POST to a PHP script for processing. However, I am looki ...

Running a service in Express.js without being dependent on incoming requests

I have a backend application built using nodeJS and express. The architecture consists of two main files, app.js for handling express configuration, controllers, and MongoDB connection, and index.js strictly for server creation. Now, I am looking to imple ...

Unable to utilize jQuery's .append(data) function due to the need to use .val(append(data)) instead

I have been attempting to utilize JQuery .append(data) on success in order to change the value of an input to the appended data like this: .val(append(data)), but it doesn't seem to be working. Surprisingly, I can successfully change the value to a st ...

Angular 1.5 - Component for fetching HTML content with dynamic data

Help needed with using Angular Component method. Developing a main html file with its main controller containing a JSON list of client data: clients: [{ "name": "John Jackson", "age": "21", "hair": "brown", }, { "name": "Janet Doe", ...

I am currently working on obtaining images that are saved by their URL within a PHP file. These images are located within a directory named "images."

My code is incomplete and not functioning as expected. $.get("museums.php",function(data,status){ var response=''; //console.log(data); var json = $.parseJSON(data); museums = json.museums; for(let m in museums) { $("#na ...

Transmit information to the controller using jQuery in a C# MVC environment

Here is my jQuery script code snippet. The script works perfectly and stores the data array/object in a variable called dataBLL. var dataBLL = []; $('#mytable tr').each(function (i) { dataBLL.push({ id: $(this).find('td:eq(0)').text( ...

I'm interested in launching a complex web application with multiple tiers on AWS, but I need guidance on how to configure it properly

I'm seeking guidance on setting up a multi-tiered web application that includes database, app, web server, and client tiers. I'm struggling with how to separate the app tier and web server tier while keeping the app tier in a private subnet. One ...

Using C# to deserialize data from Firebase

My challenge lies within a windows form application where I am attempting to deserialize an intricate Firebase response. The structure of the response is as follows: { "Mails": { "A0": {"ID":"<a href="/cdn-cgi/l/email-protection" cl ...

Analyzing BitPay Exchange Rates using Ruby

Struggling with understanding how to interpret BitPay's JSON API, specifically the /api/rates endpoint. My current setup is as follows: require 'json' require 'open-uri' data = JSON.parse(open("https://bitpay.com/api/rates").read ...

Having trouble with Grunt and Autoprefixer integration not functioning properly

Joining a non-profit open source project, I wanted to contribute by helping out, but I'm struggling with Grunt configuration. Despite my research, I can't seem to figure out why it's not working. I am trying to integrate a plugin that allow ...

Having issues with angular-file-upload failing to include the file in the request

I am currently using angular-file-upload in order to upload an image to a server that is utilizing Node and Express 4.0. However, I am encountering difficulties in accessing the file data on the server. Below is the relevant code snippet: <input type= ...