0

My use case.

  1. First I insert a single record to the grn_master table.
  2. Then I get that insertedId to insert multiple records to the grn_detail table.

This is my code.

async function create(req, res, next) {
    const grn = req.body.grn;
    const supplier = req.body.supplier_id; //An object contains objects
    const location = req.body.location;
    try {
        let query = "INSERT INTO grn_master SET customer_supplier_id = ?,location_id =?";
        connection.query(query, [supplier, location], (error, results) => {
            if (error) {
                console.log(error);
                res.sendStatus(500);
            } else {
                const grn_number = results.insertedId;
                let query = "INSERT INTO grn_detail SET item_id=?,unit_price=?,qty=?,total=?,grn_master_id=??";

                connection.query(query, [grn, grn_number], (error, results) => {
                    if (error) {
                        res.sendStatus(500);
                        console.log(error);
                    } else {
                        res.sendStatus(200);

                    }
                })


            }
        })
    } catch (error) {
        console.log(error);
        res.sendStatus(500);
    }
}

How do I achieve this using Mysql-js

Zoe
  • 27,060
  • 21
  • 118
  • 148
margherita pizza
  • 6,623
  • 23
  • 84
  • 152
  • What did you mean 'insertedId' ? be more specific – Roy G Jul 22 '18 at 07:54
  • Please provide examples of what we should expect: `grn`, `supplier`, and `location` to look like and contain. What keys do they use, do the keys match your table field names, how many nested objects does each contain? In your second query you provide a setup with 5 `?`s but then only provide an array containing 2 entries. If there are 5 `?`, there should be 5 entries in the array - or you should simply use `SET ?` and ensure your object keys match your table field names. – dusthaines Jul 23 '19 at 18:28

0 Answers0