0

How do I use the information and pass it to the backend to retrieve something from the database?

code in site:

 {Object.values(data)?.map((input) => (
      <button
        onClick={() => {
      axios.get('/api/user', input).then(function (response) {
        alert(response.reading);
      });


    }}
  >
    {input}
  </button>
))}

code in node

import { db } from '../../lib/firebase';

export default async function handler(req, res) {
  let store = {};

  console.log(req.body, 'hello');
  const user = await db.collection('readings').doc(req.body).get();
  if (!user.exists) {
    return res.status(404).json({});
  }

  return res.status(404).json({ id: user.id, ...user.data() });
Alexander Hemming
  • 753
  • 1
  • 6
  • 28
  • What is the information that you need to pass in this case? – Dharmaraj Mar 01 '22 at 15:48
  • i need to pass the input from the map. it is a button that you click and it sends the information on the button to the backend – Alexander Hemming Mar 01 '22 at 15:51
  • In general, you don't attach `bodies` to a GET request. You certainly *can*, but it isn't useful. If you need to pass info to your GET endpoint, do it via the URL. – Gaëtan Boyals Mar 01 '22 at 15:53
  • so should I use a post request? how would I go about doing this? does a post still give me information? – Alexander Hemming Mar 01 '22 at 15:56
  • What is the input? Is it just a string or something? can you log and share it? All you need to do is pass document ID as in the provided code. That can be done by query params – Dharmaraj Mar 01 '22 at 16:02
  • the input is a string, it is taken from the button on the frontend. pass the document id? doc(req.body). I'm using post request and it seems to almost work. – Alexander Hemming Mar 01 '22 at 16:05

1 Answers1

0

HTTP methods have a meaning, and it is usually very straightforward:

  • GET requests are meant to GET something from the server (back-end)
  • POST requests are meant to POST something to the server (back-end)
  • ...

See here for more information about GET request, and more broadly, the HTTP 1.1 RFC.

Starting from there, the question you need to ask yourself is:

What do I want to do with the server? Get something from it? Post something for it to save it to the database? Delete a record from the db?

Seeing your back-end code, it seems you want to GET (retrieve) something from the server and send it back to the front-end.

One of the way to do it is with URL parameters. Your URL could look something like this: http://localhost:3000/api/users?first_name=Alexander&last_name=Hemming

From that, on the server side:

import { db } from '../../lib/firebase';

export default async function handler(req, res) {
  let store = {};
  //depending on what framework you use to route, this could differ
  const {params} = req;

  // should display an object with first_name and last_name as keys
  console.log(params);
  const user = await db.collection('readings').doc(params).get();
  if (!user.exists) {
    return res.status(404).json({});
  }

  return res.status(404).json({ id: user.id, ...user.data() });
Gaëtan Boyals
  • 1,193
  • 1
  • 7
  • 22
  • doing this: {Object.values(data)?.map((input) => ( ))} the console.log does not fire – Alexander Hemming Mar 01 '22 at 16:30