Reactjs 在React中提交两个表单。一个返回字符串,另一个返回json
我正在为React中的一个站点构建我的第一个管理区域。我用的是猫鼬和猫鼬。当我提交表单将艺术家添加到db时,它会正确地执行所有操作,并且可以看到它以JSON对象的形式返回数据。当我提交一个表单,向艺术家添加一件艺术品时,它看起来很有效,但实际上没有向数据库添加任何内容,因为数据是以字符串形式发送的。我相信唯一的区别是一条路线是一个帖子,另一条是一个补丁。我找不到其他的区别。如何使表单以JSON而不是字符串的形式发送数据 我的模型Reactjs 在React中提交两个表单。一个返回字符串,另一个返回json,reactjs,mongodb,mongoose,axios,Reactjs,Mongodb,Mongoose,Axios,我正在为React中的一个站点构建我的第一个管理区域。我用的是猫鼬和猫鼬。当我提交表单将艺术家添加到db时,它会正确地执行所有操作,并且可以看到它以JSON对象的形式返回数据。当我提交一个表单,向艺术家添加一件艺术品时,它看起来很有效,但实际上没有向数据库添加任何内容,因为数据是以字符串形式发送的。我相信唯一的区别是一条路线是一个帖子,另一条是一个补丁。我找不到其他的区别。如何使表单以JSON而不是字符串的形式发送数据 我的模型 const mongoose = require("mo
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const artistSchema = new Schema({
first_name: { type: String, required: true },
last_name: { type: String, required: true },
artist_painter: { type: Boolean, required: false},
artist_sculptor: { type: Boolean, required: false},
artist_other: { type: Boolean, required: false},
featured_image: { type: String, required: false},
artwork:[
{
type: { type: String, required: false},
title: { type: String, required: false},
width: { type: Number, required: false},
length: { type: Number, required: false},
depth: { type: Number, required: false},
image: { type: String, required: false},
media: { type: String, required: false},
price: { type: Number, required: false},
newRelease: { type: Boolean, required: false}
}
]
});
const Artist = mongoose.model("Artist", artistSchema);
module.exports = Artist
API路线
// Matches with "/api/artists"
router.route("/")
.get(artistsController.findAll)
.post(artistsController.create);
// Matches with "/api/artists/:id"
router
.route("/:id")
.get(artistsController.findById)
.put(artistsController.update)
.delete(artistsController.remove);
// Matches with "/api/artists/art/:id"
router
.route("/art/:id")
.patch(artistsController.addArtById)
module.exports = router;
艺术家控制器
const db = require("../models");
// Defining methods for the ArtistsController
module.exports = {
findAll: function(req, res) {
db.Artist
.find(req.query)
.then(dbModel => res.json(dbModel))
.catch(err => res.status(422).json(err));
},
findById: function(req, res) {
db.Artist
.findById(req.params.id)
.then(dbModel => res.json(dbModel))
.catch(err => res.status(422).json(err));
},
create: function(req, res) {
db.Artist
.create(req.body)
.then(dbModel => res.json(dbModel))
.catch(err => res.status(422).json(err));
},
update: function(req, res) {
db.Artist
.findOneAndUpdate({ _id: req.params.id }, req.body)
.then(dbModel => res.json(dbModel))
.catch(err => res.status(422).json(err));
},
remove: function(req, res) {
db.Artist
.findById({ _id: req.params.id })
.then(dbModel => dbModel.remove())
.then(dbModel => res.json(dbModel))
.catch(err => res.status(422).json(err));
},
addArtById: function(req, res) {
db.Artist
.findOneAndUpdate({ _id: req.params.id }, {$push: { artwork: req.body } } )
.then(dbModel => res.json(dbModel))
.catch(err => res.status(422).json(err));
},
};
我认为您只需要一个主体解析器,我希望您已经有了一个主体解析器,但是从这里使用内部解析器:要解析表单dataOk,我尝试添加它,但它仍然没有改变任何东西。我可能把它放错地方了。我的addartist请求上的内容类型始终为json,但我的addArt请求上的内容类型始终为text/html。我想这可能就是问题所在。不知道如何更改该内容类型,也不知道为什么在补丁请求中会有所不同。