[英]Node.js 400 Bad request
我在尝试发布到mongo db时遇到了400错误的请求,不知道代码出了什么问题。
这是猫鼬的结构:
var exp = mongoose.model('Exp', { _creator: { type: mongoose.Schema.Types.ObjectId, required: true }, exps: [{ description: { type: String, required: true }, skillId: { type: mongoose.Schema.Types.ObjectId, required: true } }] });
这是我的测试用例结构,也是我想要存储数据的方式:
const exp = { _creator: UserOneId, // an ObjectID exps:[{ description: "Ate an apple", skillId: SkillOneId // an ObjectID },{ description: "Took a shower", skillId: SkillTwoId // an ObjectID }], };
exp部分应该是一个数组,以允许存储多个exp,每个exp具有描述和技能ID。
以下是我的POST函数:
app.post('/exps', authenticate, (req, res) => { var exp = new Exp({ _creator: req.user._id, // got from suthenticate middleware exps: req.body.exps }); exp.save().then(() => { res.send(exp); }, (e) => { res.status(400).send(e); }); })
和我的测试用例:
describe('POST /exps', () => { it('Should create new exp', (done) => { request(app) .post('/exps') .set('x-auth', users[0].tokens[0].token) .send(exps) .expect(200) .end(done); }) });
有了这样的结构,我无法弄清楚出了什么问题,这让我在这里未提到的400,中间件和变量与其他测试用例一起通过了,所以我认为不是这些。
测试中的错误消息如下所示:
1) POST /exps Should create new exp:
Error: expected 200 "OK", got 400 "Bad Request"
at Test._assertStatus (node_modules/supertest/lib/test.js:250:12)
at Test._assertFunction (node_modules/supertest/lib/test.js:265:11)
at Test.assert (node_modules/supertest/lib/test.js:153:18)
at Server.assert (node_modules/supertest/lib/test.js:131:12)
at emitCloseNT (net.js:1552:8)
at _combinedTickCallback (internal/process/next_tick.js:77:11)
at process._tickCallback (internal/process/next_tick.js:104:9)
任何帮助表示赞赏。
啊,找到了。 这是POST功能中的错字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.