【发布时间】:2019-03-07 22:36:04
【问题描述】:
我正在尝试使用React 中的axios 向后端发送一个整数以执行一些计算。但是,在后端,我得到了[object object]。我试图通过 console.log(Object.getOwnPropertyNames(req.body)) 查看对象中的内容,它返回空对象
我也在使用正文解析器。我的另一条路线是使用 axios 发送数据(对象),它工作正常。在body.parse中整数不被视为纯整数吗?
const express = require('express');
const app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.post('/buy', (req, res) => {
MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
if (err) throw err;
var dbo = db.db("wallet");
var myquery = { amount: {$gt: 0} }
console.log("New Amount:" + req.body)
console.log(Object.getOwnPropertyNames(req.body))
var newquery = { $set: {amount: req.body}}
/*dbo.collection("account").updateOne(myquery, newquery, function(err, re) {
if (err) throw err;
console.log("Wallet updated")
db.close();
});*/
});
});
我的反应:
handleSubmit(e){
e.preventDefault()
var remain = this.state.wallet - this.state.total
console.log(remain)
axios({ method: 'post', url: '/buy', data: remain})
}
【问题讨论】:
标签: javascript node.js reactjs axios