I'm writing a unit test with supertest to test my server. But one of my body fields containing an array of json objects 'arrives' undefined.
the code:
//declaration of variable
tags = [{name: 'tag1'},{name: 'tag2'},{name: 'tag3'}];
//actual post
agent.post('/pictures')
.set('Connection', 'keep alive')
.set('Content-Type', 'application/x-www-form-urlencoded')
.field('picTitle', 'Picture Title')
.field('tags', tags)
.attach('file', __dirname + '/img/noel.jpg')
.end(function(pictureSaveErr, pictureSaveRes) {
//do stuff
}
So the problem is that req.body.tags at the server is undefined. Strings are no problem. The actual implementation with angular frontend is working perfectly so the problem isn't with the server.
Hope someone can help me out, a big thanks in advance...
Seems what field
method does not accept arrays. Because it uses form-data module under the hood.
You should try something like this:
agent.post('/pictures')
.set('Connection', 'keep alive')
.set('Content-Type', 'application/x-www-form-urlencoded')
.field('picTitle', 'Picture Title')
.field('tags[0][name]', tags[0].name)
.field('tags[1][name]', tags[1].name)
.field('tags[2][name]', tags[2].name)
.attach('file', __dirname + '/img/noel.jpg')
.end(function(pictureSaveErr, pictureSaveRes) {
//do stuff
}