0

I want to pass multiple query objects with res.render() inside my route.js. I have a select.js which contains the SQL statements and delivers the objects to my route.js. This works fine until I want to pass multiple query objects with res.render(). Any ideas on how I can pass multiple objects at once?

snippet route.js (I need to pass get_PriceData here as well)

I already query get_KategorieData but I have no clue how to handle multiple queries in one route.

router.get('/edit', (req, res, next) => {
  var speisenEintragenData = {};
  db.get_KategorieData()
    .then(({ data: kategorie_data }) => {
      res.render('speiseEintragen', { kategorie_data }); //maybe putting res.render() after the db.get?
    })
    .catch((error) => {
      console.log(error);
    });
});

select.js


const db = require('./config');

//KATEGORIEN LADEN
const get_KategorieData=()=>{
  var sql = 'SELECT * FROM Kategorie';
  return new Promise((resolve,reject) => {
    db.query(sql, function (err, data, fields) {
      if (err) reject(err);
      resolve({data});
    });
  })
}

//PREISE LADEN

const get_PriceData=()=>{
  var sql = 'SELECT * FROM preise';
  return new Promise((resolve,reject) => {
    db.query(sql, function (err, data, fields) {
      if (err) reject(err);
      resolve({data});
    });
  })
}

module.exports={
  get_KategorieData,
  get_PriceData
}
3
  • this might help? stackoverflow.com/questions/52245174/… Commented Mar 2, 2021 at 9:12
  • Yeah I found this post as well but I use this querys in different routes individualy. If there is no possible solution inside my route.js I can try implementing a new own combined query for this particular site Commented Mar 2, 2021 at 9:19
  • hmm yeah if I was you maybe I would be looking at SQL joins, see if i can write an SQL query which joins both Kategorie and Preise. Although I don't know your DB schema so not sure if thats possible for you Commented Mar 2, 2021 at 9:22

1 Answer 1

3

There are two ways to go about this. One is to stick with promises and other is to use async/await. Using promise Create a new function to query database. This is if the module you are using does not support async/await and requires a callback.

const query = ( sql ) => {
  return new Promise(( resolve, reject) => {
    db.query(sql, function (err, data, fields) {
      if (err) reject(err);
      resolve(data);
    });
  })
}
// and then you can write an async/await function to call n queries like
const get_data = async () => {
  const sql1 = '...';
  const a = await query( sql1 );
  const sql2 = '...';
  const b = await query( sql2 );
  ....
  ....
  ....
  const sqln = '...';
  const n = await query( sqln );
  return { a ,b,... ,n};
}

Or with async/await you can directly call db.query and use the response

 const get_data = async () => {
    const sql1 = '...';
    const res_1 = await db.query(sql1);
    const sql2 = '...';
    const res_2 = await db.query(sql2);
    return { a: res_1 ,b: res_2 };
  }

router.js can rewritten as

router.get('/edit', async (req, res, next) => {
  const {a:rename_a,b:rename_b and so on}=await db.get_data();
  res.render('view', { rename_a,rename_b and so on })
});
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your solution! But now I struggle with my route.js, the object is undefinied. Do you know how to implement it inside my route.js?
Are you getting the results from the individual queries?
Now I get two Objects "[object Object] [object Object]". How can I access the values inside? I tried rename_a[0] but this doesn't work
umm... do it like this... in router.js Add const util=require('util'); on the first line. and rewrite function call to like this const res = await db.get_data(); and then console.log(util.inspect(res,false,null,true)) This will let you know what's inside the response.
I found values but unfortunately they are undefined.
|

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.