DDavis25
DDavis25

Reputation: 1309

How to make multiple queries in one promise

I'm using React to send data over to my PotgreSQL database using NodeJS. Data is getting inserted fine on the first INSERT as well as the second INSERT but my only problem is that on the .then after the second INSERT it doesn't give me the response?

const addData = (request, response) => {
const uuid = uuidv4(); 

db.pool.query('INSERT INTO albums (title, date, description, id) VALUES ($1, $2, $3, $4) ON CONFLICT (id) DO NOTHING RETURNING *' , [request.body.title, request.body.date, request.body.description, uuid])
    .then(res => {
      console.log('INSERT ' + JSON.stringify(res.rows[0]));
    }).then(() => {
       for (let i = 0; i < request.body.files.length; i++) {
        db.pool.query('INSERT INTO songs (id, name, link, index) VALUES ($1, $2, $3, $4) ON CONFLICT (album_id, index) DO NOTHING RETURNING *', [uuidv4(), request.body.files[i].name, request.body.files[i].link, request.body.files[i].index])
        }}).then(res => {
      console.log('INSERT INTO songs ' + JSON.stringify(res));
    }).catch(error => console.log(error));

}

What am I doing wrong to not get the response for the second INSERT?

Upvotes: 0

Views: 1503

Answers (1)

Tl;DR

This should get your code to work:

const addData = (request, response) => {
  const uuid = uuidv4();
  db.pool.query('INSERT INTO albums (title, date, description, id) VALUES ($1, $2, $3, $4) ON CONFLICT (id) DO NOTHING RETURNING *', [request.body.title, request.body.date, request.body.description, uuid])
    .then(res => {
      console.log('INSERT ' + JSON.stringify(res.rows[0]));
    }).then(() => {
      const dbQueryPromises = [];
      for (let i = 0; i < request.body.files.length; i++) {
        dbQueryPromises.push(db.pool.query(
          'INSERT INTO songs (id, name, link, index) VALUES ($1, $2, $3, $4) ON CONFLICT (album_id, index) DO NOTHING RETURNING *',
          [uuidv4(),
          request.body.files[i].name,
          request.body.files[i].link,
          request.body.files[i].index]));  
      }
      return Promise.all(dbQueryPromises);
    }).then(res => {
      console.log('Array of INSERT result for second insert: ', res);
    }).catch(error => console.log(error));
}

Longer explantion:

You did not get a response on the second insert is because you did not return any Promise back to the output. The first then works because what db.pool.query does is returning a Promise.resolve(res) of your function call. However, your second insert does not return any thing.

Take a look at this part of your code:

() => {
      for (let i = 0; i < request.body.files.length; i++) {
        db.pool.query('INSERT INTO songs (id, name, link, index) VALUES ($1, $2, $3, $4) ON CONFLICT (album_id, index) DO NOTHING RETURNING *', [uuidv4(), request.body.files[i].name, request.body.files[i].link, request.body.files[i].index])
      }
    }

-> This is an arrow function, and in javascript if you do not return anything, if you just gave out null.

Aside from that, you are actually creating multiple promises at once, I would suggest for you to use `Promise.all) if you want wait for all the promise to work.

Upvotes: 1

Related Questions