2

I am having problem in one of my api using express on nodejs when that connected to postgres db. here is sample of my code

const Router = require('express-promise-router');
const pool = require('../db/pool');
const router = new Router();

module.exports = router;

router.put('/:id', async (req,res) => {
    const client = await pool.connect();

    try {
        //await client.query('BEGIN');

        const queryText = "UPDATE product SET categoryid=$1, ownerid=$2, title=$3, description=$4, price=$5, stockquantity=$6 WHERE id=$7";
        const result = await client.query(queryText, [req.body.categoryid,req.body.ownerid,req.body.title,req.body.description,req.body.price,req.body.stockquantity,req.params.id]);

        //await client.query('COMMIT');

        const { updatedProduct } = await pool.query("SELECT * FROM product WHERE id = $1 LIMIT 1", [req.params.id]);

        res.json({
            success: true,
            message: "Product updated",
            updatedProduct: updatedProduct
        });
    } catch (error) {
        await client.query('ROLLBACK');

        res.status(500).json({
            success: false,
            message: error.message
        });
    } finally {
        client.release()
    }
});

the updatedProduct variable is always returning undefined, anyone having a solution for this?

1 Answer 1

1

The query returns an object with property rows, you have to use rows variable in object destructuring.

Example:

const Router = require('express-promise-router');
const pool = require('../db/pool');
const router = new Router();

module.exports = router;

router.put('/:id', async (req,res) => {
    const client = await pool.connect();

    try {
        //await client.query('BEGIN');

        const queryText = "UPDATE product SET categoryid=$1, ownerid=$2, title=$3, description=$4, price=$5, stockquantity=$6 WHERE id=$7";
        const result = await client.query(queryText, [req.body.categoryid,req.body.ownerid,req.body.title,req.body.description,req.body.price,req.body.stockquantity,req.params.id]);

        //await client.query('COMMIT');

        const { rows } = await pool.query("SELECT * FROM product WHERE id = $1 LIMIT 1", [req.params.id]);

        res.json({
            success: true,
            message: "Product updated",
            updatedProduct: rows
        });
    } catch (error) {
        await client.query('ROLLBACK');

        res.status(500).json({
            success: false,
            message: error.message
        });
    } finally {
        client.release()
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

ahh.. yes, thats the problem was.. still new to this es6 things.. thanks a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.