मैंने हर जगह देखा है कि मैं इसका समाधान ढूंढ सकता हूं। मुझे मिली एकमात्र चीज एक अनुत्तरित पोस्ट है। अगर मैंने कुछ अनदेखा किया है तो मैं क्षमा चाहता हूं।नोड.जेएस/एक्सप्रेस/मोचा/सुपरटेस्ट रेस्ट एपीआई - खाली अनुरोध निकाय
समस्या यह है कि जब मैं एपीआई में 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
अनुरोध के निकाय में नहीं है?
आप अनुरोध (यूआरएल), जहां यूआरएल परिभाषित किया गया है है है? यह एक ऐप है जिसमें आप पहले शामिल हैं? – alfonsodev
ओह, ठीक है। मैंने पोस्ट करने से ठीक पहले इसे बदल दिया था। मैंने प्रत्येक व्यक्तिगत परीक्षण में इसे पास करने के बजाय संपूर्ण अनुरोध के लिए यूआरएल सेट करने के लिए शीर्ष की तरफ लाइन जोड़ दी। मैंने यूआरएल प्रविष्टियों को हटा दिया। धन्यवाद –
ध्यान में रखें कि() इसे केवल एक बार बुलाया जाता है, पहली बार कॉल किया जाता है, यह परीक्षण समाप्त होता है। – alfonsodev