1

I'm using Insomnia to test requests to my API. Can someone explain why the request in the first image works, but the second image only posts the first object in the array?

enter image description here

enter image description here

For reference, here's the array of objects I'm trying to post:

[{
    "period":5,
    "uploadDate":"2015-11-19T21:00:00.000Z",
    "transferCode":"23100","vendor":"Unity State",
    "voucherNumber": "0000047538",
    "description":"1003-1495 Condi Operating Oct                  ",
    "amount":7083

},
{
    "period":5,
    "uploadDate":"2015-11-19T21:00:00.000Z",
    "transferCode":"23100",
    "vendor":"Northern Bahr-el State",
    "voucherNumber":"0000047546",
    "description":"1003-1494 Condi Operating Oct                  ",
    "amount":7083

}]

As well as the code I'm using in Nodejs:

//transferController.js in Node
function createTransfer(request, response) {

    console.log('posting');
    console.log('body: ' + request.body);                         //TEST
    console.info("Body: " + JSON.stringify(request.body));        //TEST

    var transfer = new Transfer(request.body);

    transfer.save(function(error) {

       // console.log('transfer and transfer: ' + transfer);
       if(error) return response.json({ message: 'could not create transfer because ' + error });
       response.json({ transfer: transfer });
  });
}
4
  • try reversing the order of the elements respective postion in the array and see whether the resulting state is exactly the same. it may not be. Commented Jan 14, 2016 at 14:19
  • I put it in reverse order and it still posts the first object, in this case just the object with "vendor": "Northern Bahr-el Ghazal" Commented Jan 14, 2016 at 14:24
  • IMO - you need to look for server-side bug in the iterator on that array. Commented Jan 14, 2016 at 14:32
  • Ok, I'll look into it. Thanks! Commented Jan 14, 2016 at 14:34

1 Answer 1

1

Since you are posting an array of objects, you should iterate through them when saving to the db. Try this (using async.mapSeries):

var async = require("async");

function createTransfer(request, response) {

    async.mapSeries(request.body, function (item, cb) {

        var transfer = new Transfer(item);

        transfer.save(function(error){
            cb(error, transfer);
        });
    }, 
    function (error, transfers){

        response.json(error ? { message: "could not create because " + error } : transfers);
    });   
}

[EDIT]

Here's an example app using this code.

main.js

var express    = require("express");
var bodyParser = require("body-parser");
var mongoose   = require("mongoose");
var async      = require("async");
var Transfer   = require("./models/Transfer");

var app = express();
app.use(bodyParser.json());

mongoose.connect("mongodb://localhost/test");

//----------------------------------------------------
app.post('/', function (req, res) {

    async.mapSeries(req.body, function iterator(item, cb) {

        var transfer = new Transfer(item);
        transfer.save(function(error){
            cb(error, transfer);
        });
    }, 
    function done(error, transfers){
        res.json(error ? { message: "could not create transfer because " + error } : transfers);
  });
});


app.listen(3000);

Transfer.js

var mongoose = require('mongoose');

var TransferSchema = new mongoose.Schema({
    uploadDate    : Date,
    period        : Number,
    transferCode  : String,
    voucherNumber : String,
    vendor        : String,
    description   : String,
    amount        : Number
});

module.exports = mongoose.model('Transfer', TransferSchema);

Output (using insomnia)

[
    {
        "__v": 0,
        "period": 5,
        "uploadDate": "2015-11-19T21:00:00.000Z",
        "transferCode": "23100",
        "vendor": "Unity State",
        "voucherNumber": "0000047538",
        "description": "1003-1495 Condi Operating Oct                  ",
        "amount": 7083,
        "_id": "5697c87e0dbd4a7413000001"
    },
    {
        "__v": 0,
        "period": 5,
        "uploadDate": "2015-11-19T21:00:00.000Z",
        "transferCode": "23100",
        "vendor": "Northern Bahr-el State",
        "voucherNumber": "0000047546",
        "description": "1003-1494 Condi Operating Oct                  ",
        "amount": 7083,
        "_id": "5697c87e0dbd4a7413000002"
    }
]
Sign up to request clarification or add additional context in comments.

3 Comments

I've never really worked with async before. I tried the code you wrote and unfortunately it did not work. When posting any number of objects in an array it only saved the __v and _id keys.
The code does work. Just posted an example server using it, please test again.
My apologies! It works very well. Thanks for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.