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?
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 });
});
}
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?
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 });
});
}
Share
Improve this question
edited Jan 14, 2016 at 14:12
cviejo
4,39820 silver badges30 bronze badges
asked Jan 14, 2016 at 13:59
Joshua SwissJoshua Swiss
3051 gold badge6 silver badges18 bronze badges
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. – Robert Rowntree 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"
– Joshua Swiss Commented Jan 14, 2016 at 14:24 - IMO - you need to look for server-side bug in the iterator on that array. – Robert Rowntree Commented Jan 14, 2016 at 14:32
- Ok, I'll look into it. Thanks! – Joshua Swiss Commented Jan 14, 2016 at 14:34
1 Answer
Reset to default 1Since 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"
}
]
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744767716a4592562.html
评论列表(0条)