首页 文章

如何在Express中访问我的req.body data / json?

提问于
浏览
2

我是node.js的新手,我试图从post请求访问我的node.js服务器上的json,所以我可以将它发送到API并将其反馈给我的前端js文件 . 我可以看到json对象,但在阅读了一些文档/ stackoverflow帖子后,我似乎无法访问它(例如:req.body.name) .

这是我的server.js文件和包的邮寄路线:

var prettyjson = require('prettyjson');
    var express = require('express');
    var http = require('http');
    var cors = require('cors');
    var bodyParser = require('body-parser');
    var app = express();


    // create application/json parser 
    app.use(bodyParser.json());
    // create application/x-www-form-urlencoded parser 
    app.use(bodyParser.urlencoded({
        extended: true
    }));

    app.post('/', function(req, res) {

    var test = req.body; //If I req.body.name here, it will return undefined
    console.log(test);

    });

这是我的前端map.js文件发布功能和数据:

var locations = [
  {name:'Le Thai', coords:{lat:36.168743, lng:-115.139866}},
  {name:'Atomic Liquors', coords:{lat:36.166782, lng:-115.13551}},
  {name:'The Griffin', coords:{lat:36.168785, lng:-115.140329}},
  {name:'Pizza Rock', coords:{lat:36.17182, lng:-115.142304}},
  {name:'Mob Museum', coords:{lat:36.172815,lng:-115.141242}},
  {name:'Joe Vicari’s Andiamo Italian Steakhouse', coords:{lat:36.169437, lng:-115.142903}},
  {name:'eat', coords:{lat:36.166535, lng:-115.139067}},
  {name:'Hugo’s Cellar', coords:{lat:36.169915, lng:-115.143861}},
  {name:'Therapy', coords:{lat:36.169041, lng:-115.139829}},
  {name:'Vegenation', coords:{lat:36.167401, lng:-115.139453}}

  ];
   //convert array to JSON 
   var jsonStr = JSON.stringify(locations);

   $.post('http://localhost:3000/', jsonStr, function(data){
     //empty for now

   },'json');

最终目标:我希望能够访问我的数据,如req.body.name . 我尝试在req.body上使用typeof,它返回一个对象,但我似乎无法访问此对象 . 我尝试使用JSON.parse,但实现了req.body已经是一个对象了 . 我想最终将这些数据提供给Yelp API .

来自console.log(req.body)的当前输出(每个请求):

{ '{"name":"Le Thai","coords":{"lat":36.168743,"lng":-115.139866}},
{"name":"Atomic Liquors","coords":{"lat":36.166782,"lng":-115.13551}},
{"name":"The Griffin","coords":{"lat":36.168785,"lng":-115.140329}},
{"name":"Pizza Rock","coords":{"lat":36.17182,"lng":-115.142304}},
{"name":"Mob Museum","coords":{"lat":36.172815,"lng":-115.141242}},
{"name":"Joe Vicari’s Andiamo Italian Steakhouse","coords":
{"lat":36.169437,"lng":-115.142903}},{"name":"eat","coords":
{"lat":36.166535,"lng":-115.139067}},{"name":"Hugo’s Cellar","coords":
{"lat":36.169915,"lng":-115.143861}},{"name":"Therapy","coords":
{"lat":36.169041,"lng":-115.139829}},{"name":"Vegenation","coords":
{"lat":36.167401,"lng":-115.139453}}': '' }

2 回答

  • 2

    问题是你're not telling the server you'重新发送它JSON,所以它没有得到解析 . 另外,作为rsp pointed out,要访问名字,您需要 req.body[0].name ,而不是 req.body.name .

    $.post 上的 dataType 参数不是要告诉服务器你发送的是什么,而是希望从服务器回来的.1129514_ . 要告诉服务器您要发送的内容,请使用 $.ajaxcontentType 选项:

    $.ajax({
        url: 'http://localhost:3000/',
        type: "POST",
        contentType: "application/json",  // <====
        data: jsonStr,
        success: function(data){
         //empty for now
        }
    });
    

    现在, body-parser 模块在请求中看到内容类型,并为您解析它 . 例如,如果我更改您的服务器文件来执行此操作:

    app.post('/', function(req, res) {
        req.body.forEach(function(entry, index) {
            console.log(index, entry.name)
        });
    });
    

    ...然后通过上面对客户端代码的更改,我在服务器控制台上得到了这个:

    0 'Le Thai'
    1 'Atomic Liquors'
    2 'The Griffin'
    3 'Pizza Rock'
    4 'Mob Museum'
    5 'Joe Vicari’s Andiamo Italian Steakhouse'
    6 'eat'
    7 'Hugo’s Cellar'
    8 'Therapy'
    9 'Vegenation'
    
  • 4

    你正在使用一个数组,所以它不会是:

    req.body.name
    

    但是例如

    req.body[0].name
    

    您可能希望迭代使用 .forEachfor 循环等获得的数组 .

相关问题