2017-08-25 15 views
0

AngularJSを使用してサーバーにかなり簡単なPOST要求を送信しようとしています。要求が通過し、バックエンドでコントローラにヒットしますが、何らかの理由でreq.dataundefinedとして表示されています。AngularJS POST要求からNode.jsのデータが未定義

フロントエンドコントローラ:

function CardDirectiveController($http, $scope) { 
    var self = this; 

    self.addToFavorites = function() { 
     let card = { 
      id: $scope.$id, 
      attack : $scope.attack, 
      cost: $scope.cost, 
      img: $scope.img, 
      name: $scope.name, 
      class: $scope.class, 
      rarity: $scope.rarity, 
      type: $scope.type 
     } 

     return $http({ 
      method: 'POST', 
      url: '/card', 
      data: card 
     }) 
    }; 
} 
angular.module('cardDirective').controller('CardDirectiveController', CardDirectiveController); 

サーバー

'use strict'; 

let express = require('express'), 
    path = require('path'), 
    router = require('./routes/sampleRouter'), 
    cardRouter = require('./routes/cardRouter'); 


let app = express(); 

// Serve any requests for static files (like index.html) 
app.use(express.static(path.join(__dirname + '/public/'))); 

// Use any routing rules found in the router file 
app.use('/', router); 
app.use('/card', cardRouter) 



app.listen(PORT, function() { 
    console.log('Application live and running on port: ' + PORT); 
}); 

ルータ:

'use strict'; 

let express = require('express'), 
    cardController = require('./../controllers/cardController'); 

let router = express.Router(); 

router.route('/').post(cardController.postCard); 
router.route('/:cardName').get(cardController.showCards); 


module.exports = router; 

バックエンドコントローラ

'use strict'; 

let cardController = { 
    showCards: showCards, 
    postCard: postCard 
}; 

module.exports = cardController 


function showCards(req, res) { 
    console.log('showCards ', req.params.cardName); 
    res.end() 
} 

function postCard(req, res) { 
    console.log('postCard ', req.url, req.method, req.data) 
    res.end() 
} 

私はこの要求を行ってから、コンソールに取得しています応答がpostCard/POST undefinedです。コンソールロギングでは、cardオブジェクトは予期した結果を返します。私は何かが分からないように感じるが、私はしばらくの間立ち往生しているように感じる。

+0

を以下に変更し ' –

答えて

2

リクエスト本体を解析するには、bodyParserミドルウェアを使用する必要があります。 req.dataの代わりにお使いのコントローラの使用req.body

var bodyParser = require('body-parser'); 

// parse application/json 
app.use(bodyParser.json()); 

:app.jsで

$ npm install body-parser 

を設定し、それ:

body-parserモジュールを取り付け

function postCard(req, res) { 
    console.log('postCard ', req.url, req.method, req.body); 
    res.end(); 
} 
0

body-使用してくださいあなたのapp.jsファイルのパーサー。 // www.npmjs.com /パッケージ/ボディ-parser``:あなたのserver.jsファイルが `` `httpsを..あなたのapp.jsにボディパーサーミドルウェアを使用してくださいコード

'use strict'; 

    let express = require('express'), 
     path = require('path'), 
     router = require('./routes/sampleRouter'), 
     cardRouter = require('./routes/cardRouter'), 
     bodyParser = require('body-parser'); 




    let app = express(); 

    // Serve any requests for static files (like index.html) 
    app.use(express.static(path.join(__dirname + '/public/'))); 
    // parse application/json 
    app.use(bodyParser.json()); 

    // Use any routing rules found in the router file 
    app.use('/', router); 
    app.use('/card', cardRouter) 



    app.listen(PORT, function() { 
     console.log('Application live and running on port: ' + PORT); 
    }); 
関連する問題