gpt4 book ai didi

javascript - 无法通过 REST 客户端发送数组中的多个对象(Insomnia)

转载 作者:太空宇宙 更新时间:2023-11-04 00:45:53 28 4
gpt4 key购买 nike

我正在使用 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 });
});
}

最佳答案

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

[编辑]

这是一个使用此代码的示例应用。

ma​​in.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"
}
]

关于javascript - 无法通过 REST 客户端发送数组中的多个对象(Insomnia),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34791471/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com