I'm stuck with the 400 bad request when trying to POST to my mongo db, not knowing what's wrong with the code.
This is the structure in Mongoose:
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
}
}]
});
This is my test case structure and it is how I want the data to be stored:
const exp = {
_creator: UserOneId, // an ObjectID
exps:[{
description: "Ate an apple",
skillId: SkillOneId // an ObjectID
},{
description: "Took a shower",
skillId: SkillTwoId // an ObjectID
}],
};
The exps part should be an array to allow storing multiple exps, each with a description and a skill id.
Below is my POST function:
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);
});
})
and my test case:
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);
})
});
With a structure like this, I just can't figure out what went wrong that's giving me the 400, middleware & variables not mentioned here have passed with other test cases so I don't think it's those.
The error message in test looks like this:
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)
Any help appreciated.
Ah, found it. It was a typo in the POST function.