admin管理员组文章数量:1397578
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"
}
]
本文标签: javascriptCan39t send multiple objects in array via REST client (Insomnia)Stack Overflow
版权声明:本文标题:javascript - Can't send multiple objects in array via REST client (Insomnia) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744767716a2624142.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论