javascript - POST http:localhost:3000undefinedpost 404 (Not Found) - Stack Overflow

I'm tried to fatch my api data from backend by using axios. I'm getting an error, like this:

I'm tried to fatch my api data from backend by using axios. I'm getting an error, like this:

POST http://localhost:3000/undefined/post 404 (Not Found)

API routes like this:

// route middleware
app.use("/api", portRoutes);

// passing on controllers
router.post("/post", create);

// rest of code will have in controller folder

Now I have tried to work in frontend

I have tried by this way:

.env file

REACT_APP_API = http://localhost:8000/api

I dont know why does not access my server side links

handleSubmit function

const handleSubmit = (e) => {
    e.preventDefault();

    // access data from backend
    axios
      .post(`${process.env.REACT_APP_API}/post`, { title, content, user })
      .then((response) => {
        console.log(response);
        setState({ ...state, title: "", content: "", user: "" });
        alert(`Post Title ${response.data.title} is created`);
      })
      .catch((error) => {
        console.log(error.response);
        alert(error.response.data.error);
      });
  };

I'm sure my api is ok, I have checked my api with postman software.

I'm tried to fatch my api data from backend by using axios. I'm getting an error, like this:

POST http://localhost:3000/undefined/post 404 (Not Found)

API routes like this:

// route middleware
app.use("/api", portRoutes);

// passing on controllers
router.post("/post", create);

// rest of code will have in controller folder

Now I have tried to work in frontend

I have tried by this way:

.env file

REACT_APP_API = http://localhost:8000/api

I dont know why does not access my server side links

handleSubmit function

const handleSubmit = (e) => {
    e.preventDefault();

    // access data from backend
    axios
      .post(`${process.env.REACT_APP_API}/post`, { title, content, user })
      .then((response) => {
        console.log(response);
        setState({ ...state, title: "", content: "", user: "" });
        alert(`Post Title ${response.data.title} is created`);
      })
      .catch((error) => {
        console.log(error.response);
        alert(error.response.data.error);
      });
  };

I'm sure my api is ok, I have checked my api with postman software.

Share Improve this question edited Mar 2, 2021 at 17:52 Alamin asked Mar 2, 2021 at 17:17 AlaminAlamin 2,1852 gold badges18 silver badges38 bronze badges 10
  • 2 If you are sending a query to http://localhost:3000/undefined/post that means that process.env.REACT_APP_API is undefined – Saddy Commented Mar 2, 2021 at 17:21
  • I don't know why show me undefined it should be api – Alamin Commented Mar 2, 2021 at 17:24
  • 2 Your code is running in a browser, process.env.REACT_APP_API is from node.js which is on the server side. You can't access server side env vars from client side js directly. – SuperStormer Commented Mar 2, 2021 at 17:29
  • 1 @Sheikh then there are a few debugging tips stackoverflow./a/53237511/553073 – lastr2d2 Commented Mar 3, 2021 at 2:03
  • 1 Actually, I forgot re-start my client server + my backend server was stop – Alamin Commented Mar 3, 2021 at 2:23
 |  Show 5 more ments

3 Answers 3

Reset to default 4

You cant access .env file from server side in your frontend app.

My suggestion is create in your frontend a config axios file

import axios from 'axios';

const api = axios.create({
  baseURL: 'http://localhost:8000/api',
});

export default api;

Then in your handleSubmit function:

const handleSubmit = (e) => {
    e.preventDefault();

    // access data from backend
    api
      .post(`/post`, { title, content, user })
      .then((response) => {
        console.log(response);
        setState({ ...state, title: "", content: "", user: "" });
        alert(`Post Title ${response.data.title} is created`);
      })
      .catch((error) => {
        console.log(error.response);
        alert(error.response.data.error);
      });
  };

I have solved my error from this link Please feel free read this article if you faced same problem, that I have faced.

Thank you.

this is also due to .env file in src folder... move it outside of src folder to the main client folder

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744169650a4561473.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
能,也可根据模型 hook 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>