当我使用发布请求并有响应(true)时出现错误,然后未定义,为什么?

时间:2019-09-19 09:30:13

标签: reactjs axios

  import { BrowserRouter as Router, Route, Switch } from "react-router 
  dom";
  import Navbar from "./components/layouts/Navbar";
  import Landing from "./components/layouts/Landing";
  import Posts from "./components/posts/Posts";



 <Router>
  <Navbar />
  <Route exact path='/' component={Landing} />
  <section className='container'>
    <Alert />
    <Switch>
      <Route exact path='/register' component={Register} />
      <Route exact path='/login' component={Login} />
      <Route exact path='/profiles' component={Profiles} />
      <Route exact path='/profile/:id' component={Profile} />
      <PrivateRoute exact path='/dashboard' component={Dashboard} />
      <PrivateRoute exact path='/posts' component={Posts} />
      <PrivateRoute
        exact
        path='/create-profile'
        component={CreateProfile}
      />
      <PrivateRoute exact path='/edit-profile' component={EditProfile} />
      <PrivateRoute exact path='/add-education' component={AddEducation} 
       />
      <PrivateRoute
        exact
        path='/add-experience'
        component={AddExperience}
      />
       </Switch>
         </section>
         </Router>

enter image description here

我有UserProfileApi.putProfile(this.state.selectedShoe, this.state.selectedWear, this.state.dreams, this.state.newAvatarPhoto, this.state.Sports).then(response => { console.log(response); })

200 OK时我只有(true)答案
我该如何解决这个问题? enter image description here

这是另一个文件UserProfileApi.js,此处全部请求等 enter image description here 我的axios现在看起来像

MyProfilePage.jsx?8853:156 Uncaught (in promise) TypeError: Cannot read property 'then' of undefined

there is something strange

2 个答案:

答案 0 :(得分:1)

putProfile函数不会返回任何内容,因此,如果您执行putProfile(..).then,则确实会尝试不进行任何调用.then

要解决此问题,请在putProfile中添加return语句,行279: return axios.put(...)

答案 1 :(得分:1)

这是您的问题:您尝试在不返回promise的函数上定义.then块:)

您有2个选择:

  1. 转换putProfile函数以返回promise- return axios.put(...)。 注意:您应该先删除.then子句中的内容。 .then是多余的,因为您可以在外部.then中获得相同的响应值。
  2. 然后将要移到.then函数内部的putProfile移动。仅当您不需要在预期的putProfile子句中使用.then函数之外的任何东西时,这种情况才会发生。