0

inside a get request fucntion to mysql db and After two awaits on promises to get two lists (tab1 and tab2) filled, i want to get the intersection between them. tab1 and tab2 are lists of strings.

const app = express();
var intersectionBetween = function(a1,a2){
  return  a1.filter(function(n) { return a2.indexOf(n) !== -1;});
};
app.get('/getPackages', async (req, res) => {
  var tab1= []
  var tab2= []
  const promiseIntersection = (a1, a2) => {
    return new Promise((res, rej)=> {
      intersectionBetween(a1, a2, function (err, resultsss){
        if (err) rej(err);
        return res(resultsss)
      });
    })
  }
  const intersectionb = await promiseIntersection(tab1, tab2);
  console.log("efe",intersectionb)
  res.send('Posts fetched...');

});


app.listen(3000, () => console.log("Express server is runnig at port: 3000"))

I wanted to get the intersection but when I go in the browser under '/getPackages' the browser runs forever without returning anything. Can anyone help?

sffgsfgs
  • 61
  • 1
  • 1
  • 7
  • just get the intersection in the SQL query – pilchard May 06 '22 at 04:26
  • 1
    `intersectionBetween` just accepts two parameter, but when invoking it you're passing three parameters, so eventually `res or rej` will not get called at all – Code Maniac May 06 '22 at 04:28
  • @pilchard the source of the table is not the same. the first one is coming from db and the second from svn repo – sffgsfgs May 06 '22 at 04:28
  • @CodeManiac how do you suggest correcting this? – sffgsfgs May 06 '22 at 04:34
  • @sffgsfgs I assume you need to pass results only if you found intersection, so if you found intersection resolve promise, if not then just reject promise. – Code Maniac May 06 '22 at 04:36
  • @CodeManiac can you please write your suggestion? I didn't get it. Thanks!! – sffgsfgs May 06 '22 at 05:13
  • https://stackoverflow.com/a/46957719/7927294 check this out – sb39 May 06 '22 at 05:52
  • @sffgsfgs get those two arrays from your db first and do the traversal in your nodejs runtime, code is very complex for the problem you are trying to solve here – sb39 May 06 '22 at 05:55
  • @sffgsfgs , you are resolving promise in the callback method of ```intersectionBetween``` method which you are not invoking. So the promise will get never resolved. – gvmani May 06 '22 at 05:58

1 Answers1

0

After two awaits on promises to get two lists (tab1 and tab2) filled, you can directly call it and get intersection. There is nothing async.

app.get('/getPackages', async (req, res) => {
        var tab1= []
        var tab2= []
        const intersectionb = intersectionBetween(tab1, tab2);
        console.log("efe",intersectionb)
        res.send('Posts fetched...');
    
    });
Leon
  • 131
  • 4
  • thank you. That's what I tried before this solution but i get an empty array which shouldn't be the case – sffgsfgs May 06 '22 at 11:50
  • I don't know what is empty array. From my test given 2 arrays of strings the function works fine, unless there are no duplicated strings in them. Could you show us how to get tab1 and tab2, and what's the value inside them? – Leon May 06 '22 at 13:24