Tips for extracting specific field titles from a RESTful API with the help of ExpressJS and Axios

Recently, I have been working on some code that allows me to retrieve data from an external API. Below is an example of the code:

//endpoint to fetch data from an external API
app.get("/externalapi", (req, res) => {
    
    let apiURL = 'https://herokuapp.com/api/v1/data';
      
      axios.get(apiURL)
          .then(response => {
              
              res.status(200).json(response.data);
          })
          .catch((err) => {
              res.status(500).json({ message: err });
          });
  });

One of the sample data entries retrieved via Postman is shown below:

{
    "data": [
        {
            "first_name": "Fiona",
            "last_name": "Smith",
            "phone_number": "987-3595-89",
            "mental_health_referral": false,
            "date_last_mental_health_referal": "02/09/2018T00:00:00.0000Z",
            "legal_councel_referal": true,
            "CHW_id": 6866318
        },
        {
            "first_name": "Richard",
            "last_name": "Stewart",
            "phone_number": "281-0394-41",
            "mental_health_referral": true,
            "date_last_mental_health_referal": "03/23/2018T00:00:00.0000Z",
            "legal_councel_referal": false,
            "CHW_id": 9241074
        },
        {
            "first_name": "Andrew",
            "last_name": "Stevens",
            "phone_number": "068-8173-37",
            "mental_health_referral": true,
            "date_last_mental_health_referal": "03/30/2018T00:00:00.0000Z",
            "legal_councel_referal": true,
            "CHW_id": 9241074
        }
}

My current objective is to extract only Fiona's information, which is the first set of data. I have designed a URL template for this purpose:

GET https://herokuapp.com/api/v1/data/{{first_name}}/{{last_name}}/{{phone_number}}

I attempted to use the following URL:

https://herokuapp.com/api/v1/data?first_name=Fiona&last_name=Smith&phone_number=987-3595-89

However, I have not achieved the desired outcome. Even when I send the get request in Postman, it still returns all the results from the API. What could be the missing piece here?

Answer №1

In my opinion, it's best to include the parameters directly in the API call, similar to this:

 axios.get(apiURL, { params: { username: 'JohnDoe', email: 'johndoe@email.com', age: 25 } }

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

Divide the cookies on the webpage

Today I was working on some coding tasks when I encountered an error: Cannot read properties of undefined (reading 'split') at getCookie ((index):38:49) at (index):47:31 The section of my code where the issue occurred (beginning at line ...

An issue arises when trying to upload a file using axios; the req.body and req.file objects are

Below is the front-end code snippet: let file = this.$refs.file.files[0] //"this" pertains to vue let data = new FormData(); data.append("image", file, file.fileName); data.append("content", this.content); data.append("title", this.title); data. ...

Tips for reusing a form within a one-page website

I am looking for a way to handle a form's submit action using JavaScript. My goal is to hide the form when it is submitted and then be able to reuse it later. However, the code I currently have seems to have a hidden state that prevents the submit act ...

Please provide either a render prop, a render function as children, or a component prop to the Field(auto) component

While working on my project and implementing an Auto complete feature using final-form, I encountered the following error: Must specify either a render prop, a render function as children, or a component prop to Field(auto) In order to resolve this issue ...

Differences between Mongoose's updateOne and save functions

When it comes to updating document records in a MongoDB database, there are several approaches to consider. One method involves defining the User model and then locating the specific user before making modifications and saving using the save() method: let ...

What could be causing the issue with lodash throttle not functioning correctly in the useWindowSize custom hook?

I'm attempting to implement a resize event with throttle, but I'm encountering an issue. To troubleshoot, I have tried the following: import {throttle} from 'lodash' export function useWindowSize() { const [windowSize, setWindowSize] ...

Retrieve the value of a DOM element within the document.ready function

After researching, I discovered a way to make my textArea resize when the page is fully loaded. $(document).ready(function() { // Handler for .ready() called. }); I decided to test it out and inserted the following code into the function: $(document). ...

Learn how to remove data from a React JS application without causing a page refresh by utilizing the useLoaderData() function in conjunction with React Router

I am working on preventing my table from refreshing with the new version of userLoadData from react-router-dom@6 after deleting some data. In an attempt to achieve this, I created a function called products() within useLoaderData. While this function succ ...

Verify whether an object possesses all the attributes of a class in TypeScript

Within my typescript code, I have a class called abc: export class ABC{ public a : any; public b : any; public c? : any; public d? : any; } In one of my functions, I receive an input which is represented as data:any. My goal is to verify i ...

Real-time functionality is not supported by Firebase functions

I've set up a firebase query within a method in VueJS: data: {this.todaysEvents}, methods : { getTodaysEvents (day) { this.todaysEvents = [] const events = db.ref('calendar') const query = events.orderByChild('da ...

How can I resize an element using jQuery resizable and then revert it back to its original size with a button click?

I need help figuring out how to revert an element back to its original size after it has been modified with .resizable. I attempted the following: <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="//code. ...

When working on a REST APIs project with ReactJS fetch and NodeJS, I am encountering difficulties in reading authorization headers on the server side

I'm having issues retrieving headers on the server side using ReactJS fetch to call an API on the front end. In my old project, this functionality was working perfectly fine. I'm puzzled as to why it's not working now, especially since I hav ...

What does the `Class<Component>` represent in JavaScript?

Apologies for the lackluster title (I struggled to think of a better one). I'm currently analyzing some Vue code, and I stumbled upon this: export function initMixin (Vue: Class<Component>) { // ... } What exactly does Class<Component> ...

Reordering a pair of items within an array using ReactJS

After pondering, I wondered if there exists a neat and tidy method to swap two objects within an array while utilizing setState. Here's my current approach: export function moveStepUp(index) { if(index > 0){ let currentStep = this.stat ...

Passing an array from the PHP View to a JavaScript function and plotting it

Greetings, I am currently facing the following tasks: Retrieving data from a database and saving it to an array (CHECK) Sending the array from Controller to View (CHECK) Passing that array to a JavaScript function using json_encode (CHECK) Plotting the ...

Bootstrap datepicker not applying datepicker-options as expected

I have been trying to pass all my options in a JSON file according to the instructions on http://angular-ui.github.io/bootstrap/#/top, but unfortunately, I haven't been able to get it to work. I've been experimenting with the Plunkr provided on t ...

In the following code, to what is the term "this" pointing?

While following a tutorial, I stumbled upon a piece of code that I am having trouble understanding. Can someone please help me decipher this code? I have marked my questions within the comments. Code UserSchema.pre('save', function(next){ // ...

Is it possible to scroll by using the dragenter event?

Looking for a way to achieve incremental scroll up and scroll down using jQuery without jQuery UI? Here's the scenario - I have two divs: <div class="upper" style="height:35px;background-color:red;right:0;left:0;top:0;position:fixed;width:100%;z-i ...

Utilize React's debounce feature in conjunction with updating state using

Introduction Let's discuss the popular debounce function provided by lodash. Imagine a scenario where a user rapidly enters values like 1, 12, 123, 1234. The debounce function ensures that only one alert, with the final value 1234, is triggered afte ...

Error: NullPointerExpection - Trying to execute 'void com.mobiledealer.dao.OrderDAO.insertOrUpdate(com.mobiledealer.model.Order)' on a reference to an object that does not exist

Hey there, I'm having an issue with the response from a REST API. I receive a JSON object, parse it successfully, but when I try to add it to Realm I get a Null Pointer Exception. You can find my project here: https://github.com/666Angelus666/MobileDe ...