我对React很陌生,目前正在尝试弄清楚如何使用axios.post请求更新JSON文件。我有一个功能,用户可以上传照片,目标是将照片的ID和imgURL添加到存储数据的photos.json中。 这是我的帖子功能:
count
我在照片控制器中将路由发布到了create函数,如下所示:
val approxCount = df.rdd.countApprox(timeout = 1000L,confidence = 0.95).getFinalValue().high
val numOfPartitions = Math.max(Math.round(approxCount / 100), 1).toInt
df.repartition(numOfPartitions)
这是我猜到的问题所在,因为老实说,我不太确定应该在此处输入什么代码。任何帮助将不胜感激!!!
答案 0 :(得分:1)
在您的create函数中,您需要将保存的文档作为JSON返回到前端,以便您的前端可以使用它
axios.post('/api/photos', image)
.then(res => {
console.log(res.data);
//=>consume your JSON response
})
.catch(function (error) {
console.log(error);
})
module.exports.create = (req, res) => {
const photo = new Photo({
id: req.body.id,
imgURL: req.body.imgURL
})
photo.save()
.then((resp) => {
//send_code_success(res,201, "photo save success");
return res.status(200).json(resp) //=> return your saved doc to front end
})
.catch((err) => {
send_code_error(res,500, "photo save error");
console.error("Could not save photo to database:", err);
})
};