How can I pass a variable from my post to my get?

ProgrammerGuy

I'm using react/express.

I have a simple webpage, which takes user input and stores this in SQL using axios.post. In my backend I need to access this user variable in my get, so I can filter what to send back to the user.

I have not been able to get the return function to work and I don't want to use global variables as it has caused errors before.

Is there something i'm missing? How can I access a variable from my get request from my post request.

frontend.js

import React from 'react';
import { useState, useEffect } from "react";
import Calendar from 'react-calendar';
//import 'react-calendar/dist/Calendar.css';
import Axios from 'axios'; 

const CalendarApp = () => {
  var [selectedDate, newDate] = useState(new Date());
  const [event, newEvent] = useState("")
  const [userEvent, setUserEvent] = useState("")
  const [userEventList, setUserEventList] = useState([])
  const [newUserEvent, setNewUserEvent] = useState("")

//Add event to DB
  const addUserEvent = () => {
    var userDate = (selectedDate + "").slice(3, 16);
    console.log("From addUserEvent", userDate)
    if (userEvent === "") {
      //alert("Please enter event")
    }
    else {
      Axios.post("http://localhost:3001/send", {
        userEvent: userEvent, userDate: userDate
      }).then(() => {
        //setUserEventList([...userEventList, { userEvent: userEvent, userDate: userDate}])
      });
      // changes the event list depending on what date the user clicks 
      document.getElementById('ui').value = '';
      setUserEvent(userEvent => userEvent = "")
    }
  };

  const getUserEvent = (userDate) => {
    Axios.get("http://localhost:3001/getData").then((response) => {
      setUserEventList(response.data)
      console.log("response.data: ", response.data)
    });
  };
}

backend.js

//Setting up express, similar to flask
const express = require('express'); 
const app = express() 
const mysql = require('mysql'); 
const cors = require('cors')  

//Setting up SQL, hide password properly 
const db = mysql.createConnection({
    host:'localhost',
    user: 'root',
    password: '******',
    database: 'calendardb',
});

app.use(cors());
app.use(express.json()); 

// insert information in to DB
app.post('/send',(req,res) =>{
    const userEvent = req.body.userEvent
    const userDate = req.body.userDate //<---- Access this variable from the get below
    currentDate = req.body.userDate
    console.log("FROM DB ADD",userDate,"d",currentDate)
    db.query('INSERT INTO calevent (userEvent, userDate) VALUES (?,?)', 
    [userEvent,userDate], (err,result) =>{
        if(err){
            console.log(err)
        } else{
            res.send("Data send to DB")
        }
    }
    );
  });

//Getting Specific  data from DB
app.get("/getData",(req, res) =>{
    db.query("SELECT * FROM calevent WHERE userDate = ?", [userDate], (err, result) => {
        if(err){
            console.log(err)
        }else{
            res.send(result)
        }
        console.log(result)
    });
});
Jappan

REST calls are stateless, which means each request has to be independent of the state. If you're aiming for a RESTful service, I'd suggest not doing this. Although, saving it in session (express-session) or something similar can work but I'd suggest you to add the userDate in the get call as well as a param (since GET request won't have a body). You already are sending an unused argument userDate to the getUserEvent function, something similar to this.

Axios.get("http://localhost:3001/getData", { params: { userDate }} )

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how can I pass php variable from my select to jquery?

How I can pass this variable to use it from my module?

How can I get a variable from terminal to use it in my script?

How do I pass a variable from my View to my Controller?

How can i pass a variable from my adapter to a fragment i instantiate in Kotlin

How do I get my data from my DB as a variable?

How can I prevent caller to my function from using the same pass-by-reference variable in C++?

How can i pass my variable values from console application to windows form application

How can I pass a Laravel variable into my AngularJS view?

How can I pass my angularjs variable to Php page and retrive it

How can I pass a python variable to my template via flask?

How can I post my data that i get from SQL into divs in php

How can I get my ID from button to my controller?

How do I pass a variable from a shell script to my .muttrc?

How do I run a function on each post in my list view and pass a boolean variable into my html?

How can I pass data from angular to my cloud function and get a response back?

How can I get an order id from Firestore and pass it to my adaptor class?

How can I get post id from my html form to views.py

How can I pass data from a service into my controller?

How can I protect my site from the multiple post requests?

How can I get my bash script to remove the first n and last n lines from a variable?

PowerShell - How can I get output in the shell to list all existing accounts from my variable without my script terminating first?

How can I get my code to return the correct variable?

Is there a 'variable_get' method? If not, how can I create my own?

How can I save in a variable a portion of my cURL GET command?

I can't get my httpOnly cookie from my post request using axios

Why can't I get the gallery block from my post type to show in my wordpress api

In Angular how can I pass the response of my post request as one of the parameters to go into another post request?

How can I share a variable from a thread to my main in python?