-1

I need your help for a "simple" problem but the promise is again a mistake for me...

This is un exemple of my code.

 // In a middleware, i have a mongoose request

Request
.find({ user: req.user.email })
.then(function (allDatasets) {
    allDatasets.forEach(function (oneDataset) {
        if (oneDataset.use_case == reqParams.use_case) {
            res.status(400).json({
                'msg': 'My response'
            })
        }
    })
})

// And my code continue...

I would like that if if condition is true, node return this response and stop the code ! But.. Node is asynchronous and while my loop is running, the code below is executed.

I tried to put the code below in a .then () but it does not wait for the end of the loop to continue to execute the code ...

Thank you for your help

David
  • 21
  • 6
  • you maybe need to clarify your issue.... maybe you want to make the `function (oneDataset)` an `async function (oneDataset)` and then await some code? – Matthias Herrmann Apr 16 '18 at 20:55
  • @MatthiasHerrmann you can't stop a forEach by returning. https://stackoverflow.com/questions/6260756/how-to-stop-javascript-foreach – Kevin B Apr 16 '18 at 21:02
  • I'm not entirely sure what the problem you have is. The for each definitely can cause a problem if there's more than one thing in it that satisfies the requirements of the response. but because you mentioned promises, i wonder if maybe you'r problem is related to a loop that isn't present in your code sample... otherwise, promises would be irrelevant to your problem. – Kevin B Apr 16 '18 at 21:04
  • I want that Node wait the total execution of forEach before continuing... – David Apr 16 '18 at 21:06
  • @Zarathoustra75 Why? that makes no sense given the code in your question. Nothing in the forEach is worth waiting for. – Kevin B Apr 16 '18 at 21:06
  • Also... Why does the forEach in your question exist? couldn't you have just made that if statement part of your mongoose query the same way req.user.email is? – Kevin B Apr 16 '18 at 21:10
  • @kevinB If use_case exists, I go out and return that answer. My code is finished. Otherwise, if it does not exist, I have a whole treatment to do on my file. So if, it is important to wait because otherwise forEach is useless since the rest of the code runs anyway. – David Apr 16 '18 at 21:11
  • You'll have to return to the promise a value that represents the fact that none were found, and chain off of it recursively until one is. But your question doesn't contain enough code for anyone to help you with that. – Kevin B Apr 16 '18 at 21:12
  • Thank you for your help @kevinB Finally a use this plan. //Before the code return request.findOne({user: user, use_case: use_case}), function(err, obj){ if (err){ console.log(err) } if (obj != null){ My response } .then(function(){ The reste of code, only execute if obj is null }) – David Apr 16 '18 at 21:55

2 Answers2

2

I think what you actually want is

Request.find({ user: req.user.email })
.then(function (allDatasets) {
    if (allDatasets.some(oneDataset => oneDataset.use_case == reqParams.use_case)) {
        res.status(400).json({
           'msg': 'My response'
        })
    } else {
        // And my code continue...
    }
});
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Well tried but I have a lot of code below with functions and this method breaks all the rest of the code. I would like to use NodeJS methods to solve my problem. – David Apr 16 '18 at 21:03
  • Well if you have a lot of code that needs to wait for the `Request.find()`, then you have a lot of code that needs to go inside the `then` handler. (Or in a function that is called from there, of course). There's no way around that. – Bergi Apr 16 '18 at 21:14
  • Maybe `async`/`await` syntax can simplify your code, but conceptually that's the same. – Bergi Apr 16 '18 at 21:15
0

Best way, you can use async function const allDatasets = await Request.find({ user: req.user.email });

allDatasets.forEach(function (oneDataset) {
    if (oneDataset.use_case == reqParams.use_case) {
        res.status(400).json({
            'msg': 'My response'
        })
    }
})
//you can continue your code here
Ro Bi
  • 36
  • 2