我到处寻找我能找到的解决方案。我发现的唯一一件事是一个未答复的帖子。如果我忽略了某些事情,我深表歉意。
问题是当我尝试获取APIPOST
中的值时/createQuestion
,正文为空/未定义。我Cannot read proprety 'question' of undefined
从 API 收到这样的错误。
快递 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
将其添加到请求的正文中吗?