APIを作成しました。
Postmanで目的のjsonデータを取得することができます。
フロントエンドでAxiosを用いると取得できません。
しばらく調べましたが、原因がわからなかったの教えていただきたいです。
よろしくお願いします。
Postman
GET http://localhost:4000/api/money/month
json
1{ 2 "date": "2022-09-11", 3 "userId": 1 4}
結果
json
1[ 2 { 3 "id": 1, 4 "userId": 1, 5 "amount": 1200, 6 "memo": "食費", 7 "bool": true, 8 "date": "2022-09-11T00:00:00.000Z", 9 "createdAt": "2022-09-19T17:31:42.000Z", 10 "updatedAt": "2022-09-19T17:31:42.000Z", 11 "UserId": 1 12 }, 13 { 14 "id": 2, 15 "userId": 1, 16 "amount": 1200, 17 "memo": "食費", 18 "bool": true, 19 "date": "2022-09-11T00:00:00.000Z", 20 "createdAt": "2022-09-19T17:31:50.000Z", 21 "updatedAt": "2022-09-19T17:31:50.000Z", 22 "UserId": 1 23 }, 24]
バックエンドAPI
js
1// 月単位でレコードを取得 2router.get("/month", async (req, res) => { 3 try { 4 const data = await Money.findAll({ 5 where: { 6 date: { [Op.substring]: req.body.date.slice(0, 7) }, 7 userId: req.body.userId, 8 }, 9 }); 10 if (!data) { 11 return res.status(200).json({ msg: "該当データが存在しませんでした" }); 12 } else { 13 return res.status(200).json(data); 14 } 15 } catch (err) { 16 return res.status(500).json(err); 17 } 18});
フロントエンド・コード
js
1const { default: axios } = require("axios"); 2 3const data = { 4 date: "2022-09-11", 5 userId: 1, 6}; 7 8const getFetch = async () => { 9 const all = await axios.get("http://localhost:4000/api/money/month", data); 10 console.log(all.data); 11}; 12 13getFetch();
結果
shell
1node:internal/process/promises:279 2 triggerUncaughtException(err, true /* fromPromise */); 3 ^ 4 5[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "AxiosError: Request failed with status code 500".] { 6 code: 'ERR_UNHANDLED_REJECTION' 7}

回答1件
あなたの回答
tips
プレビュー