无法通过 onClick 事件反应传递数据
我尝试通过单击按钮将“价格”发送到 MongoDB。后端工作正常,因为我可以通过邮递员存储数据,
React 代码:
import axios from "axios";
const Package = (props) => {
//const [price, setPrice] = useState();
//console.log(price);
const handlePayment = (e) => {
console.log(e);
const result = axios.post("http://localhost:3000/pay", e);
console.log(result);
};
return (
<>
<div className="col-lg-4 col-md-6 col-sm-10 pb-4 d-block m-auto">
<div className="pricing-item">
<div className="pt-4 pb-3">
<h4>{props.name}</h4>
</div>
<div className="pricing-price pb-1 text-primary color-primary-text ">
<h1>
<span>{props.price}</span>
</h1>
</div>
<div className="pricing-description">
<ul className="list-unstyled mt-3 mb-4"></ul>
</div>
<div className="pricing-button pb-4">
<button
onClick={() => handlePayment(props.price)}
type="button"
className="btn btn-lg btn-primary w-75"
>
Buy
</button>
</div>
</div>
</div>
</>
);
};
但是,通过 React 存储在数据库中的唯一内容是日期和 Id。
请给我一些建议,以将正确的数据传递给数据库
I tried to send "price" by clicking on the button to MongoDB. the backend works fine because I'm able to store data via postman,
the React Code:
import axios from "axios";
const Package = (props) => {
//const [price, setPrice] = useState();
//console.log(price);
const handlePayment = (e) => {
console.log(e);
const result = axios.post("http://localhost:3000/pay", e);
console.log(result);
};
return (
<>
<div className="col-lg-4 col-md-6 col-sm-10 pb-4 d-block m-auto">
<div className="pricing-item">
<div className="pt-4 pb-3">
<h4>{props.name}</h4>
</div>
<div className="pricing-price pb-1 text-primary color-primary-text ">
<h1>
<span>{props.price}</span>
</h1>
</div>
<div className="pricing-description">
<ul className="list-unstyled mt-3 mb-4"></ul>
</div>
<div className="pricing-button pb-4">
<button
onClick={() => handlePayment(props.price)}
type="button"
className="btn btn-lg btn-primary w-75"
>
Buy
</button>
</div>
</div>
</div>
</>
);
};
However, the only thing stored in the DB via React is Date and Id.
Please give me some advice to pass the right data to DB
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个
Try This