Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

143
Views
Extract key:value pair from an object sent to backend

I am trying to use the value from an object. I don't need to use the 'key' part of the object. Also, I need to remove the curly brackets { } from around the object. How can I do so ?

I'm making a searchbar that sends data into backend, but I only need to use the 'value' part of the object. Perhaps I should use query strings for that ?

const handleClick = (e) => {
    e.preventDefault() // stop page from refresh

    if (setUserInput !== '') { // is searchbar isnt empty, then run the POST method to send 
// data from {userInput} which is the searchbar
      
      const options = 
      {
        method: 'POST',
        headers: {
          'Content-Type':  'application/json'
        },
        body: JSON.stringify({userInput}),
      };
  
      const response = fetch('http://localhost:3001/search', options);
      console.log("search entered");
    };

      }

in the backend when I console.log the response it gives me

`"{ userInput: 'lollol' }"

and out of all of that I only need the 'lollol' part ( lollol is just an example, but whatever is typed into the searchbar will be shown instead of it ). As such:

const searchResult = req.body /// return lollol ✅

not

const searchResult = req.body /// return `"{ userInput: 'lollol' }" ❌

Should I use query strings to accomplish this, or is there a way to extract data from an object?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You don't need to stringify the object in the body. Replace

body: JSON.stringify({userInput}),

with

body: {userInput:userInput},

And then you can use it like req.body.userInput. Hope, it helps!!

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!