我无法从req.body node.js获取值 [英] I can't get values from req.body node.js

查看:53
本文介绍了我无法从req.body node.js获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有关node.js的帮助:我的req.body是这样填充的

I need help with node.js: I have my req.body populated like this

{ 
   '{
       "email":"mail@hostname.com",
       "password":"12345"
    }'
     : '' 
} 

但我无法获取值req.body.email和req.body.password未定义

but I can't get values req.body.email and req.body.password are undefined

我的代码是:

user.js

exports.loginByEmail= function(req, res) {
console.log('POST');
console.log(req.body);//show values
console.log(req.body.email);//undefined
console.log(req.body.password);//undefined
    User.find({email:req.body.email,password:req.body.password}).toArray(function(err, userLoged) {
    if(err) return res.send(500, err.message);
    res.status(200).jsonp(userLoged);
    });
};

app.js

var express = require("express"),
app = express(),
bodyParser  = require("body-parser"),
methodOverride = require("method-override");
mongoose = require('mongoose');

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(methodOverride());

推荐答案

在第一位设置主体的方式看起来像键是您的对象字符串,值是一个空字符串.

The way you have the body set up in the first bit, it looks like the key is your object string and the value is an empty string.

即您的身体是钥匙'{email:mail @ hostname.com,password:12345}'(为清楚起见,删除了引号)值为"

ie your body is the key '{email:mail@hostname.com,password:12345}' (quotes removed for clarity) with the value ''

尝试将您的身体写成

  {
    email: "email@email.com",
    password: "12345"
  }

这篇关于我无法从req.body node.js获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆