我正在使用React.js(前端)和Node.js(Express.js)作为后端来处理我的私人银行项目。我正在制作付款记录,需要帮助。
我创建了两条路线。其中一个返回我进行的转账,第二个返回我收到的转账。
我想将这两个JSON对象合并为一个,并在React.js组件中按日期对它们进行排序。 axios是否允许这种可能性?
我还将代码留给控制器,也许有人对如何更好地显示付款历史有更好的主意?
控制器REST API:
exports.findAllByIdRecipient = (req, res) => {
Transaction.findAll({
where: {
id_recipient: req.params.transactionRecipientId,
},
}).then(bill => {
res.send(bill);
});
};
exports.findAllByIdSender = (req, res) => {
Transaction.findAll({
where: {
id_sender: req.params.transactionSenderId,
},
}).then(bill => {
res.send(bill);
});
};
路线:
app.get('/api/transactions/recipient/:transactionRecipientId', transactions.findAllByIdRecipient);
app.get('/api/transactions/sender/:transactionSenderId', transactions.findAllByIdSender);
来自../recipient/1的JSON:
[
{
"id": 3,
"id_sender": 2,
"id_recipient": 1,
"data_time": "2018-12-08T03:49:00.000Z",
"amount_money": 30,
"transfer_title": "title"
}
]
来自../sender/1的JSON:
[
{
"id": 1,
"id_sender": 1,
"id_recipient": 2,
"data_time": "2018-12-06T16:41:30.000Z",
"amount_money": 100,
"transfer_title": "title pay"
},
{
"id": 2,
"id_sender": 1,
"id_recipient": 3,
"data_time": "2018-12-08T03:48:30.000Z",
"amount_money": 20,
"transfer_title": "title pay"
}
]
最佳答案
尝试使用传播算子
即(用您从ajax请求中获得的任何内容替换它)
let data1 = [
{
"id": 1,
"id_sender": 1,
"id_recipient": 2,
"data_time": "2018-12-06T16:41:30.000Z",
"amount_money": 100,
"transfer_title": "title pay"
},
{
"id": 2,
"id_sender": 1,
"id_recipient": 3,
"data_time": "2018-12-08T03:48:30.000Z",
"amount_money": 20,
"transfer_title": "title pay"
}
]
和
let data2 = [
{
"id": 3,
"id_sender": 2,
"id_recipient": 1,
"data_time": "2018-12-08T03:49:00.000Z",
"amount_money": 30,
"transfer_title": "title"
}
]
let combinedData = [...data1, ...data2]
然后创建一个函数来对数据进行排序
const sortingData = (data) => {
return data.sort((a, b) => {
return Date.parse(b["data_time"]) - Date.parse(a["data_time"])
})
}
然后将combinedData传递给上述函数,即
sortingData(combinedData)
关于javascript - REST API(React.js)中两个json对象的付款历史,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53679096/