前端不从后端接收任何数据(服务器发送一个对象,前端不接收任何数据)

发布于 2025-01-09 08:21:21 字数 3612 浏览 0 评论 0原文

服务器向前端发送一个对象,但前端没有收到任何内容,因此 onOrderSubmitCOD 函数在发送表单时停止,因此没有条件结果(navigate()、loadUser()),该

order.js 的任何解决方案(后端)expressjs knex js:

const handleOrderCOD = (req,res, db) => {
const {firstname, lastname, email, mobilenumber, adress, city, size, quantity} = req.body;
if(!firstname || !lastname || !email || !mobilenumber || !adress || !city || !size || !quantity)
    {return  res.status(400).json('no data to load')};
db.insert({
        
        firstname: firstname,
        lastname:lastname,
        email:email,
        mobilenumber:mobilenumber,
        adress:adress,
        city:city,
        size:size,
        quantity:quantity,
        joined: new Date()

       
    
    })

.into('orders')
.returning('*')

    .then(user =>{
        if(user){
              res.json(user[0]) 
        } else{
            res.status(400).json('unable to order1')
        }

    })
    .catch(err=>res.status(400).json('unable to order2'))
}

order.js(前端)react.js

function Order(props) {
   const navigate = useNavigate();


  const [firstName, setFirstName] = useState('');
  const [lastName, setLastName] = useState('');
  const [email, setEmail] = useState('');
  const [mobileNumber, setMobileNumber] = useState('');
  const [adress, setAddress] = useState('');
  const [city, setCity] = useState('');
  const [size, setSize] = useState('');
  const [quantity, setQuantity] = useState('1');






 function onOrderSubmitCOD  ()  {
  fetch('http://localhost:3000/orderCOD', {
      method: 'post',
      mode: 'cors',
      headers: {'Content-Type': 'application/json'},
      body: JSON.stringify({
        firstname: firstName,
        lastname:lastName,
        email: email,
        mobilenumber: mobileNumber,
        adress: adress,
        city: city,
        size:size,
        quantity: quantity

      })
    })
      .then(response => response.json())
      .then(user => {
        console.log(user)
        if (user.id) {
         
          props.loadUser(user);
          
          navigate('/orderCompleted'); 
        }
      })
  }

app.js (前端)react.js

const initialState= {
   
    user: {
      id: '',
      firstname: '',
      lastname: '',
      email:'',
      mobilenumber:'',
      adress:'',
      city: '',
      size:'',
      quantity: '',
      joined:''
    }


  }






class App extends Component {
 constructor(){
  super();
  this.state= initialState;
}
   

    loadUser = (data) => {
  this.setState( {user:
      {       id: data.id,
      firstname: data.firstname,
      lastname:data.lastname ,
      email:data.email,
      mobilenumber:data.mobilenumber,
      adress:data.adress,
      city: data.city,
      size:data.size,
      quantity: data.quantity,
      joined:data.joined
        }})
}


render() {
    return (
     <>
  <Router>
      <Routes>
        <Route path="/" element={<Navbar /> }>
        <Route index element={<Home />} />
        <Route path="order"> 
          <Route index element={<Order  loadUser={this.loadUser}/>} />
          <Route path="orderCompleted" element={<OrderCompleted />} />
        </Route>
      
          <Route path="collab" element={<Collab />} />
          <Route path="art" element={<Art />} />
         
          <Route path="*" element={<NoPage />} />
         </Route>
      </Routes>
    </Router>
<Footer/>
</>
    );
  }
 

} 

export default App;

the server send an object to the frontend but the frontend don't receive anything so the function onOrderSubmitCOD stops at sending the form , so there is no result of conditions (navigate() , loadUser()), any solution for that

order.js (backend) expressjs knex js:

const handleOrderCOD = (req,res, db) => {
const {firstname, lastname, email, mobilenumber, adress, city, size, quantity} = req.body;
if(!firstname || !lastname || !email || !mobilenumber || !adress || !city || !size || !quantity)
    {return  res.status(400).json('no data to load')};
db.insert({
        
        firstname: firstname,
        lastname:lastname,
        email:email,
        mobilenumber:mobilenumber,
        adress:adress,
        city:city,
        size:size,
        quantity:quantity,
        joined: new Date()

       
    
    })

.into('orders')
.returning('*')

    .then(user =>{
        if(user){
              res.json(user[0]) 
        } else{
            res.status(400).json('unable to order1')
        }

    })
    .catch(err=>res.status(400).json('unable to order2'))
}

order.js(front end) react.js

function Order(props) {
   const navigate = useNavigate();


  const [firstName, setFirstName] = useState('');
  const [lastName, setLastName] = useState('');
  const [email, setEmail] = useState('');
  const [mobileNumber, setMobileNumber] = useState('');
  const [adress, setAddress] = useState('');
  const [city, setCity] = useState('');
  const [size, setSize] = useState('');
  const [quantity, setQuantity] = useState('1');






 function onOrderSubmitCOD  ()  {
  fetch('http://localhost:3000/orderCOD', {
      method: 'post',
      mode: 'cors',
      headers: {'Content-Type': 'application/json'},
      body: JSON.stringify({
        firstname: firstName,
        lastname:lastName,
        email: email,
        mobilenumber: mobileNumber,
        adress: adress,
        city: city,
        size:size,
        quantity: quantity

      })
    })
      .then(response => response.json())
      .then(user => {
        console.log(user)
        if (user.id) {
         
          props.loadUser(user);
          
          navigate('/orderCompleted'); 
        }
      })
  }

app.js (frontend) react.js

const initialState= {
   
    user: {
      id: '',
      firstname: '',
      lastname: '',
      email:'',
      mobilenumber:'',
      adress:'',
      city: '',
      size:'',
      quantity: '',
      joined:''
    }


  }






class App extends Component {
 constructor(){
  super();
  this.state= initialState;
}
   

    loadUser = (data) => {
  this.setState( {user:
      {       id: data.id,
      firstname: data.firstname,
      lastname:data.lastname ,
      email:data.email,
      mobilenumber:data.mobilenumber,
      adress:data.adress,
      city: data.city,
      size:data.size,
      quantity: data.quantity,
      joined:data.joined
        }})
}


render() {
    return (
     <>
  <Router>
      <Routes>
        <Route path="/" element={<Navbar /> }>
        <Route index element={<Home />} />
        <Route path="order"> 
          <Route index element={<Order  loadUser={this.loadUser}/>} />
          <Route path="orderCompleted" element={<OrderCompleted />} />
        </Route>
      
          <Route path="collab" element={<Collab />} />
          <Route path="art" element={<Art />} />
         
          <Route path="*" element={<NoPage />} />
         </Route>
      </Routes>
    </Router>
<Footer/>
</>
    );
  }
 

} 

export default App;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文