【发布时间】:2017-11-08 17:54:30
【问题描述】:
我正在使用 npm 请求将数据插入 MongoDB - 就像使用表单变量进行直接注入一样。这是我单击提交按钮后在浏览器中看到的内容: FORM DATA
这是我用来将凭据插入数据库的脚本:
request.post(
'http://127.0.0.1:8081/process_post', {
json: {
fname: 'john',
surname:'smtith',
age:100,
password: '123as',
email: 'email@email.com',
phone: 9283746,
city: 'anywhere',
country: 'AZSW',
postal_code: 117058,
picture: 'something.jpg',
description:'description'
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body)
}
}
);
这是服务器端函数:
app.post('/process_post', urlencodedParser, function (req, res) {
var response = {fname: req.body.fname, surname: req.body.surname, age: req.body.age,
password: req.body.password,email: req.body.email, phone: req.body.phone,
city: req.body.city, country: req.body.country, postal_code: req.body.postal_code,
description: req.body.description, picture:req.body.picture};
//etc
问题是服务器把变量弄乱了,并对发送的数据进行随机匹配。此外,在 mongo shell 中,我可以看到数据以“未定义”的形式出现……是因为我正在发送 Json 并且服务器端设置为捕获变量吗?
编辑:这些是服务器的logs
【问题讨论】:
-
“服务器被变量弄乱了”,例如?以及如何将数据插入 mongo
-
猫鼬不适合你吗?
new Model.save()? -
嗯,我试过了,但没有成功,而且这个包看起来更简单
-
@Yogesh_D 我已经用控制台日志更新了帖子
-
这样做:让 reqBody = JSON.parse(req.body);然后使用 reqBody.fname... 等
标签: javascript node.js mongodb