我已经尽我所能寻找解决方案。我找到的唯一信息是一个未被回答的帖子。如果我有所忽略,请谅解。
问题在于,当我尝试在/createQuestion
API中获取POST
值时,请求体为空/未定义。我会收到类似于这样的错误:Cannot read proprety 'question' of undefined
,来自API。
Express API:
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
到请求体中?