[英]REQ.BODY being passed as empty in GET call
我有以下代碼遇到問題。 我在js中創建一個對象,然后執行API調用,將該對象傳遞給控制器。 該對象填充在js組件和API組件中,但到達控制器時為空。 我已經驗證我有app.use(bodyParser.urlencoded({擴展名:true})); 建議在我的服務器代碼中編碼為其他類似問題的答案。
js / API調用片段:
getGeoLocation = () => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(position => {
let latLng = {
lat: position.coords.latitude,
lng: position.coords.longitude,
name: "current location",
details: false,
detailsText: null,
markerIcon: currentLocation
};
let searchData = {
currentLat: latLng.lat,
currentLng: latLng.lng,
dist: this.state.dist,
limit: this.state.limit
};
console.log("in JS component:");
console.log(searchData);
API.getLocations(searchData)
.then(res =>
this.setState({ markers: res.data }),
console.log("markers: "),
console.log(this.state.markers),
this.setState(prevState => ({
currentLatLng: latLng,
markers: [latLng, ...prevState.markers]
}))
)
.catch(err => console.log(err));
console.log(this.state.markers)
});
API片段:
import axios from "axios";
export default {
// Gets all locations
getLocations: function(searchData) {
console.log("in API:");
console.log(searchData);
return axios.get("/api/locations/", searchData);
},
上面的控制台日志:
控制器片段:
module.exports = {
findAll: function (req, res) {
console.log("req.body in controller:");
console.log(req.body);
db.Locations
.findAll({
attributes: { include: [[sequelize.literal("(3959 * acos(cos(radians(" + req.body.currentLat + ")) * cos(radians(lat)) * cos(radians(lng) - radians(" + req.body.currentLng + ")) + sin(radians(" + req.body.currentLat + ")) * sin(radians(lat))))"), 'distance']] },
having:
sequelize.where(sequelize.col('distance'),
{ [Op.lte]: req.body.dist }
),
order: sequelize.col('distance'),
limit: req.body.limit
})
.then(dbModel =>
res.json(dbModel))
.catch(err => res.status(422).json(err));
},
控制台日志結果:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.