前端不从后端接收任何数据(服务器发送一个对象,前端不接收任何数据)
服务器向前端发送一个对象,但前端没有收到任何内容,因此 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论