如何解决如何将数组发布到axios
我有以下代码
const onSubmit = data => {
let params = {
experience_type: data.education[0].type,body: data.education[0].text,start_time: data.education[0].from,end_time: data.education[0].to,}
axios.post('http://localhost:5000/experience',params)
.then((response) => {
console.log(response);
},(error) => {
console.log(error);
});
};
我想使用Axios将POST作为整个data.education数组发布。目前,我只能发布第一个索引。我如何使用axios发布data.education数组的所有索引。我目前的想法是使用散布运算符并逐个传递。
解决方法
您可以使用JSON.stringify(data.education)
一次传递整个数据数组。但是您需要在后端适当地处理集合。
答案是将整个对象发送到快速路由,然后映射到对象,一次将每一行插入数据库。因此,一个Axios发布了请求,并像其他人指出的那样处理了其余的路由。
req.body.education.forEach(async values => {
const newPost = await pool.query(`INSERT INTO Experience (experience_type,start_time,end_time,user_id,author,body,date_created)
VALUES($1,$2,$3,$4,$5,$6,NOW()) RETURNING * `,[values.experience_type,values.start_time,values.end_time,USER ID HERE,USERNAME HERE,values.body]);
res.json(newPost[0]);
这是对象被映射然后插入到单独行中的方式
,这将帮助您,亲爱的同伴。
var axios = require('axios');
var qs = require('qs');
var data = qs.stringify({
'experience_type': data.education[0].type,'body': data.education[0].text,'start_time': data.education[0].from,'end_time': data.education[0].to,});
var config = {
method: 'post',url: 'http://localhost:5000/experience',headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},data : data
};
axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。