Я пытаюсь сделать сообщение из моего собственного приложения React с помощью axios. Это пост к моему экспресс-API, который я написал. Данные, которые я пытаюсь отправить, верны, но я все еще не получаю запрос об ошибке 400.
Мой код React Native с запросом axios выглядит так:
postToDatabase = async() => {
console.log(this.state);
API.post('/restaurants', {
name: this.state.name,
description: this.state.description,
places_free: this.state.places_free,
latitude: this.state.latitude,
longitude: this.state.longitude,
phone: this.state.phone,
website: this.state.website
})
.then(res => console.log(res))
.catch(err => console.log(err));
}
экспресс-сообщение выглядит так:
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.use(cors());
app.post('/api/restaurants', (req, res, next) => {
var errors = [];
if(!req.query.name){
errors.push("No name specified");
}
if(!req.query.description){
errors.push("No description specified");
}
if(!req.query.places_free){
errors.push("No description specified");
}
if(!req.query.latitude){
errors.push("No latitude specified");
}
if(!req.query.longitude){
errors.push("No longitude specified");
}
if(!req.query.phone){
errors.push("No phone specified");
}
if(!req.query.website){
errors.push("No website specified");
}
if(errors.length){
res.status(400).json({"error": errors.join(',')});
return;
}
var data = {
name: req.body.name,
description: req.body.description,
places_free: req.body.places_free,
latitude: req.body.latitude,
longitude: req.body.longitude,
phone: req.body.phone,
website: req.body.website
}
var sql = "INSERT INTO restaurants (name, description, places_free, latitude, longitude, phone, website) VALUES (?, ?, ?, ?, ?, ?, ?)";
var params = [data.name, data.description, data.places_free, data.latitude, data.longitude, data.phone, data.website];
db.run(sql, params, (err, result) => {
if(err){
res.status(400).json({"error": err.message});
return;
} else {
res.json({
"status" : "succes",
"data": data
});
}
});
});
Я надеялся, что кто-нибудь увидит, где я ошибся.