0

My problem is very simple, I need to put my data taken from the database in a variable but I don't know how to keep the variable defined. At the end of the pool the data went out and left the variable not defined.

So I have tried to make a res.send to send the data to the next :

.post('/route', data ,function(req, res){
    ...
}); 

but I think this is not a good idea.

.post('/musicbrainz/research/', function(req, result, next){
    var nomArtiste = req.body.recherche;
    var query = "SELECT idartiste * FROM artiste);";
    db.query(query, (err, res) => {
        if (err) {
            return next(err);
        }
        rows = res.rows;
    });
})

.get('/musicbrainz/results/', rows, function(req, res){
const results = rows;
    ...

My variable rows are undefined when it exits the db.query function.

ProgrammerPer
  • 1,125
  • 1
  • 11
  • 26
Nillem
  • 1
  • 1
  • Indeed, thats not a good idea. The only way I see to make that flow work would be using Redis Cache. You would use `/research` to set the cache and `/results` to read from it. – Rashomon Feb 09 '19 at 11:57
  • I'm going to try this ! Thank you ! – Nillem Feb 09 '19 at 12:14
  • The reason your variable is undefined is that you are making an asynchronous query to the database. This has nothing to do with caching etc. – Matt Morgan Feb 09 '19 at 12:37
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Matt Morgan Feb 09 '19 at 12:39
  • Actually its not clear what you want to achieve. You want to read the database at/research and set rows variable and then return rows in other url called /results? – Rashomon Feb 09 '19 at 13:44

0 Answers0