0

I could not fetch the data using axios, despite fetching it succesfully through postman. Not sure why this is happening

Here's my function in react

import axios from "axios";

const url = "http://localhost:3001/tasks/";

const getDashboardData = async (setTasks) => {
  await axios.get(`${url}`, {
    uid: sessionStorage.getItem("uid")
  })
    .then((result) => {
      console.log(result.data);

      setTasks(result.data);
    })
    .catch((error) => {
      console.log(error);
    })
}

Web app side

import React, { useContext, useEffect, useState } from 'react'
import { UserContext } from '../layout/Layout';
import { getDashboardData, handleCreate } from './DashboardScripts';

const Dashboard = () => {
  const {user, setUser} = useContext(UserContext);
  const [tasks, setTasks] = useState(null);

  useEffect(() => { getDashboardData(setTasks) }, []);

...

Here's my server side code

// Returns all tasks
router.get("/", async (req, res) => {
  let uid = req.body.uid;
  await db.collection("users").doc(uid).collection("tasks").get()
    .then((querySnapshot) => {
      let tasksTemp = [];
      querySnapshot.forEach((doc) => {
        // doc.data() is never undefined for query doc snapshots
        tasksTemp.push([doc.id, doc.data()]);
      });
      res.status(200).send(tasksTemp);
    })
    .catch((error) => {
      res.status(400).send(error.message);
    });
})

Postman screenshot Postman

I'm not sure whether I'm writing the Axios request properly, or whether I'm calling it the function wrongly from the web app. I tried different ways of writing the Axios request to no avail

Phil
  • 157,677
  • 23
  • 242
  • 245
  • Axios doesn't have a `uid` option. What is that meant to be doing? – Phil Dec 12 '22 at 01:50
  • @Phil It's the user id passed as json body – Anthony Lee Dec 12 '22 at 01:53
  • Does this answer your question? [Why express server receives front end data as undefined?](https://stackoverflow.com/questions/74607186/why-express-server-receives-front-end-data-as-undefined). See also [HTTP GET with request body](https://stackoverflow.com/q/978061/283366) – Phil Dec 12 '22 at 01:55

0 Answers0