0

Im using node js with postgresql and I'm trying to get the user's last message in each conversation. So I'm getting the data from 3 queries then adding them up and stocking them in an array so i can return it. This code is working but sometimes it returns the data just fine and sometimes some data is missing.

I think the problem is that the code takes too long to execute so it doesnt have enaugh time to return all the data. So im wondering if there is another approach or a postgresql function that I can use to optimize this code.

router.get("/api/messagesUser", verifToken, (req, res) => {
    pool.query(
        "SELECT * from public.discussion AS D where D.user = $1",
        [req.user.id],
        (err, result) => {
            if (err)
                return res
                    .status(400)
                    .send({ message: "une erreur s'est produite, veuillez réessayer." });
            else if (result.rowCount > 0) {
                const arr = [];

                result.rows.map(async (val, index) => {
                    await pool.query(
                        `SELECT * from public.boutique AS B, public."page_layout" AS P
                        WHERE B.id = P."id_boutique" 
                        AND B.id = $1`,
                        [val.boutique],
                        async (err, result2) => {
                            if (err)
                                return res.status(400).send({
                                    message: "une erreur s'est produite, veuillez réessayer.",
                                });
                            else if (result2.rowCount > 0) {
                                await pool.query(
                                    `SELECT contenu, sender, date from public.messages
                                    WHERE id_discussion = $1
                                    ORDER BY date DESC 
                                    LIMIT 1`,
                                    [val.id],
                                    async (err, result3) => {
                                        if (err)
                                            return res.status(400).send({
                                                message:
                                                    "une erreur s'est produite, veuillez réessayer.",
                                            });
                                        else if (result3.rowCount > 0) {
                                            const data = result2.rows[0];
                                            data["message"] = result3.rows[0].contenu;
                                            data["id_discussion"] = val.id;
                                            data["date"] = result3.rows[0].date;
                                            result3.rows[0].sender == req.user.id
                                                ? (data["lastSender"] = "user")
                                                : (data["lastSender"] = "boutique");

                                            await arr.push(result2.rows[0]);

                                            if (index == result.rowCount - 1)
                                                return res.status(200).send(arr);
                                        } else if (index == result.rowCount - 1)
                                            return res.status(200).send(arr);
                                    }
                                );
                            }
                        }
                    );
                });
            } else {
                res.status(200).send([]);
            }
        }
    );
});
  • 1
    I think you should examine your PostgreSQL logs to find out what is happening. You can enable logging the duration of your queries, try executing them manually and other techniques: https://stackoverflow.com/questions/9063402/get-execution-time-of-postgresql-query – Julius Tuskenis Sep 27 '21 at 12:09

0 Answers0