I want to hit my API with Axios.get

Asked 2 years ago, Updated 2 years ago, 279 views

API created successfully.
You can retrieve the desired json data from Postman.
If you use Axios at the front end, you cannot get it.
I looked into it for a while, but I didn't know the cause, so please tell me.
Thank you for your cooperation.

Postman
GET http://localhost:4000/api/money/month

{
  "date": "2022-09-11",
  "userId"—1
}

Results

[
    {
        "id"—1,
        "userId": 1,
        "amount": 1200,
        "memo": "Food expenses",
        "bool"—true,
        "date": "2022-09-11T00:00:00.000Z",
        "createdAt": "2022-09-19T 17:31:42.000Z",
        "updatedAt": "2022-09-19T 17:31:42.000Z",
        "UserId"—1
    },
    {
        "id"—2,
        "userId": 1,
        "amount": 1200,
        "memo": "Food expenses",
        "bool"—true,
        "date": "2022-09-11T00:00:00.000Z",
        "createdAt": "2022-09-19T 17:31:50.000Z",
        "updatedAt": "2022-09-19T 17:31:50.000Z",
        "UserId"—1
    },
]

Backend API

//Retrieves records by month
router.get("/month", async(req,res)=>{
  try{
    const data = wait Money.findAll({
      where: {
        date: {[Op.substring]: req.body.date.slice(0,7)},
        userId: req.body.userId,
      },
    });
    if(!data){
      return res.status(200).json({msg: "The data did not exist"});
    } else{
      return res.status(200).json(data);
    }
  } catch(err){
    return res.status(500).json(err);
  }
});

front-end code

const {default:axios} = require("axios");

const data = {
  date: "2022-09-11",
  userId: 1,
};

const getFetch=async()=>{
  constall = wait axios.get("http://localhost:4000/api/money/month", data);
  console.log(all.data);
};

getFetch();

Results

node:internal/process/promises:279
            triggerUncaughtException(err, true/* fromPromise*/);
            ^

[UnhandledPromiseRejection: This error originated ether by flowing inside of an asymmetric function without a catch block, or by rejection a promise which was not handled with.catch().The prompt rejected with the reason "AxiosError: Request failed with status {500."]
  code: 'ERR_UNHANDLED_REJECTION'
}

javascript api axios

2022-09-30 22:05

1 Answers

It's solved.
Axios passes parameters instead of body elements to the second argument.
This is the wrong part.


2022-09-30 22:05

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.