2013-10-25 14 views
12

मैंने हर जगह देखा है कि मैं इसका समाधान ढूंढ सकता हूं। मुझे मिली एकमात्र चीज एक अनुत्तरित पोस्ट है। अगर मैंने कुछ अनदेखा किया है तो मैं क्षमा चाहता हूं।नोड.जेएस/एक्सप्रेस/मोचा/सुपरटेस्ट रेस्ट एपीआई - खाली अनुरोध निकाय

समस्या यह है कि जब मैं एपीआई में POST मान प्राप्त करने का प्रयास करता हूं, तो शरीर खाली/अपरिभाषित होता है। मुझे एपीआई से आने वाले Cannot read proprety 'question' of undefined जैसी त्रुटियां मिलती हैं।

एक्सप्रेस एपीआई:

app.post("/createQuestion", function(req, res) { 
    var questionType = req.body.question.type; 
    var questionText = req.body.question.text; 
    var questionDuringClass = req.body.question.duringClass; 

    // Do a bunch of stuff 

    res.send(response); 
}); 

परीक्षण:

var should = require('should'); 
    var assert = require('assert'); 
    var request = require('supertest'); 
    var winston = require('winston'); 

    request = request('http://localhost:8080'); 

     describe('Questions', function() { // Test suite 
      before(function(done) { 
       done(); 
      }); 

     it('Should create a freeResponse question', function(done) { // Test case 
     var postData = { 
      "question" : { 
       "type" : "freeResponse", 
       "text" : "This is a test freeResponse question (automated testing)", 
       "duringClass" : "1" 
      } 
     }; 

     request() 
     .post('/createQuestion') 
     .send(postData) 
     .expect(200) 
     .end(function(err, res) { // .end handles the response 
      if (err) { 
       return done(err); 
      } 

      done(); 
     }); 
    }); 

it('Should delete a freeResponse question', function(done) { // Test case 
     var postData = { 
      "question" : { 
       "type" : "freeResponse", 
       "text" : "This is a test freeResponse question (automated testing)", 
       "duringClass" : "1" 
      } 
     }; 

     request() 
     .post('/deleteQuestion') 
     .send(postData) 
     .expect(200) 
     .end(function(err, res) { // .end handles the response 
      if (err) { 
       return done(err); 
      } 

      done(); 
     }); 
    }); 

मैं क्या याद आ रही है? .send()POST डेटा को कुछ अलग प्रारूप में भेज रहा है? क्या यह POST अनुरोध के निकाय में नहीं है?

+0

आप अनुरोध (यूआरएल), जहां यूआरएल परिभाषित किया गया है है है? यह एक ऐप है जिसमें आप पहले शामिल हैं? – alfonsodev

+0

ओह, ठीक है। मैंने पोस्ट करने से ठीक पहले इसे बदल दिया था। मैंने प्रत्येक व्यक्तिगत परीक्षण में इसे पास करने के बजाय संपूर्ण अनुरोध के लिए यूआरएल सेट करने के लिए शीर्ष की तरफ लाइन जोड़ दी। मैंने यूआरएल प्रविष्टियों को हटा दिया। धन्यवाद –

+0

ध्यान में रखें कि() इसे केवल एक बार बुलाया जाता है, पहली बार कॉल किया जाता है, यह परीक्षण समाप्त होता है। – alfonsodev

उत्तर

27

शायद यह है कि आपका ऐप बॉडी पार्सर मिडलवेयर का उपयोग नहीं कर रहा है।

app.use(express.bodyParser()); 

expressjs डॉक्स से:

req.body

यह गुण पार्स अनुरोध शरीर युक्त एक वस्तु है। यह सुविधा बॉडी पार्सर() मिडलवेयर द्वारा प्रदान की जाती है, हालांकि अन्य शरीर पार्सिंग मिडलवेयर भी इस सम्मेलन का पालन कर सकता है। यह गुण {} को डिफ़ॉल्ट करता है जब bodyParser() का उपयोग किया जाता है।

यहाँ आप एक पूर्ण उदाहरण

var express = require('express'); 
var request = require('supertest'); 

var assert = require('assert'); 
var app = express(); 

app.use(express.bodyParser()); 
app.get('/', function(req, res) { 
    res.send('ok'); 
}); 

app.post('/createQuestion', function(req, res) { 
    var message = req.body.partA + ' ' + req.body.partB; 
    res.send(message); 
}); 

describe('testing a simple application', function() { 
    it('should return code 200', function(done) { 
    request(app) 
     .get('/') 
     .expect(200) 
     .end(function(err, res){ 
     if(err) { 
      done(err); 
     } else { 
      done(); 
     } 
     }); 
    }); 

    it('should return the same sent params concatenated', function(done) { 
    request(app) 
     .post('/createQuestion') 
     .send({ partA: 'Hello', partB: 'World'}) 
     .expect(200, 'Hello World') 
     .end(function(err, res){ 
     if(err) { 
      done(err); 
     } else { 
      done(); 
     } 
     }); 
    }); 

}); 
+0

धन्यवाद। वह यह था। मैं वास्तव में इसकी प्रशंसा करता हूँ! –

+2

यह मेरे लिए भी काम किया। एक्सप्रेस 3.x और 4.x के लिए आपको _body-parser_ का उपयोग नहीं करना चाहिए .bodyParser(): 'app.use (आवश्यकता ('बॉडी-पार्सर')। जेसन())' – zayquan

संबंधित मुद्दे