简体   繁体   English

如何发布多个 Axios 请求但发布请求的数量不同

[英]How to post multiple Axios requests but the number of post request varies

A bit of context, I am trying to take a JSON file data and populate my MongoDB with the data.有点上下文,我正在尝试获取 JSON 文件数据并用数据填充我的 MongoDB。 The way I am currently doing it is as such:我目前这样做的方式是这样的:

for (let i = 0; i < data.length; i++) {
    await Axios.post("http://localhost:8080/createRawGeopoints", {
        time: data[i].time,
        lat: data[i].lat,
        lng: data[i].lng
    }).then((response) => {
        console.log("Posted")
    });
}

The length of the data object varies depending on the JSON file I am trying to read.数据object 的长度取决于我试图读取的 JSON 文件。 An example of the data object is as such data example .数据示例 object 就是这样的数据示例
However, this method is taking too long especially if I have more than 50 JSON entries I am trying to post.但是,这种方法花费的时间太长,特别是如果我尝试发布的 JSON 条目超过 50 个。
Is there another way to do this such that it can post all the data in one shot?是否有另一种方法可以一次性发布所有数据? However, it needs to take into account that the number of post requests depends on the length of the data object.但是需要考虑到post请求的数量取决于数据object的长度。 I will need to match each attribute of the data object to the schema attributes, such as time, lat and LNG.我需要将数据 object 的每个属性与模式属性(例如时间、纬度和 LNG)相匹配。

My Mongoose Model Schema where I am trying to post and populate with my data is as shown:我试图发布和填充我的数据的 ZCCADCDEDB567ABAE643E15DCF0974E503Z Model 架构如下所示:

const mongoose = require('mongoose');

const RawGeopointSchema = new mongoose.Schema({
    time: {
        type: String,
        required: true,
    },
    lat: {
        type: Number,
        required: true,
    },
    lng: {
        type: Number,
        required: true,
    },
}, {
    versionKey: false
});

const RawGeopointModel = mongoose.model("raw-geopoints", RawGeopointSchema)
module.exports = RawGeopointModel;

My API Code to POST data:我的 API 代码发布数据:

app.post("/createRawGeopoints", async (req, res) => {
    const geopoint = req.body;
    const newGeopoint = new RawGeopointsModel(geopoint);
    await newGeopoint.save();
    res.json(geopoint)
})

Your server API needs to accept an array of objects.您的服务器 API 需要接受对象数组。

It will be a lot faster because there would be only one network request/response between the server and the client.它会快很多,因为服务器和客户端之间只有一个网络请求/响应。 That also means one single axios request.这也意味着一个单一的 axios 请求。

Yes, the request body would be bigger, but even if the server is reusing the same connection, it would still take more time to parse all theses incoming HTTP requests and produce as many HTTP responses.是的,请求主体会更大,但即使服务器重用相同的连接,解析所有这些传入的 HTTP 请求并产生尽可能多的 HTTP 响应仍然需要更多时间。

Also there would eventually be a lot less communication with the database since all the objects can probably be inserted in a single request.此外,由于所有对象可能都可以插入到单个请求中,因此最终与数据库的通信会少很多。

Also, you should be careful about mixing the async |另外,你应该小心混合async | await syntax with the functional style then() |具有函数式风格的await语法then() | catch() Promises. catch()承诺。 Choose one or the other.选择一个或另一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM