无法通过REST客户端在阵列中发送多个对象(Insomnia)

时间:2016-01-14 13:59:18

标签: javascript arrays node.js api rest

我正在使用Insomnia来测试对我的API的请求。有人可以解释为什么第一个图像中的请求有效,但第二个图像只发布数组中的第一个对象吗?

enter image description here

enter image description here

供参考,这是我要发布的对象数组:

[{
    "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

}]

以及我在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 });
  });
}

1 个答案:

答案 0 :(得分:1)

由于您要发布一个对象数组,因此在保存到数据库时应该迭代它们。试试这个(使用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);
    });   
}

<强> [编辑]

以下是使用此代码的示例应用。

<强> 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);

输出(使用失眠)

[
    {
        "__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"
    }
]