Home > database >  GET request is sent twice in express JS
GET request is sent twice in express JS

Time:10-15

I have a loop, that is making 19 calls to a GET url.

generateMonthlyPNG(recap) {
    for (const meter of meters) { // 19 elements
        let url = process.env.BASE_URL   "operations/"   recap.operation.name   "/meters/"   meter   "/monthly";
        promises.push(this.makePngScreenshot(recap, url, meter, "monthly"));
    }
    return promises
}

With :

async makePngScreenshot(recap, url, meterId, filename) {
        axios.get(url, null); // Make the request to generate html page
        const destination = "public/images/"   recap.operation.name   "/"   recap.date_ini   "_"   recap.date_end   "/"   meterId
        // console.log("Creating image in: "   destination)
        return new Pageres({ delay: 2, filename: filename })
            .src(url, ['1300x650'], { crop: true })
            .dest(destination)
            .run()
}

app.get("/operations/:operation/meters/:meter/monthly", async (req, res) => { // Used to generate PNG from graph
console.log(".")
}

generateMonthlyPNG is only called once

Weird thing is that I have 19 meters, and I print 38 dots (.), so, I duplicate all my calls, I don't understand why.

Any idea ?

CodePudding user response:

It appears you are accessing the URL twice, once with:

axios.get(url)

and again with:

new Pageres({ delay: 2, filename: filename })
        .src(url, ['1300x650'], { crop: true })
        .dest(destination)
        .run()

This will cause the underlying code to request the same page twice.

  • Related