How to call model.find method synchronously in node / loopback?
Asked Answered
T

3

6

I am using a custom model and trying to filter it in a loop using find method. e.g. given below

for i = 0 to n
{
var u = User.find( where { name: 'john'});
}

It doesn't work.

Also, if I use the following

for i = 0 to n
{
User.find( where { name: 'john'}, function(u) {... } );

// How do I call the code for further processing? 
}

Is there a way to call find method synchronously ? Please help.

Thanks

Trawick answered 27/1, 2015 at 14:11 Comment(2)
What exactly do you want to accomplish with the loop?Gowen
Try using a promise. Have a look at: howtonode.org/promisesMesic
T
2

You can solve this using the each function in the async package. Example:

async.each(elements, function(element, callback) {
    // - Iterator function: This code will be executed for each element -

    // If there's an error execute the callback with a parameter
    if(error)
    {
        callback('there is an error');
        return;
    }

    // If the process is ok, execute the callback without any parameter
    callback();

}, function(err) {
    // - Callback: this code will be executed when all iterator functions have finished or an error occurs
    if(err)
        console.log("show error");
    else {

        // Your code continues here...
    }

});

This way your code is asynchronous (the iterator funcions are executed simultaneously) except the callback function that will be executed when all have finished.

The example with your code would be:

var elements = [1 .. n];

async.each(elements, function(element, callback) {

    User.find( where { name: 'john'}, function(u) {

        if(err)
        {
            callback(err);
            return;
        }

        // Do things ...

        callback();

    });

}, function(err) {

    if(err)
        console.log("show error");
    else {

        // continue processing
    }

});
Toolmaker answered 13/3, 2015 at 1:33 Comment(1)
This is not the answer (no synchronous solution).Fransen
A
1

All of those model methods (querying/updating data) are asynchronous. There are no synchronous versions. Instead, you'll need to use the callback function that you pass as the second argument:

for (var i = 0; i<n; ++i) {
    User.find( {where: { name: 'john'} }, function(err, users) {
        // check for errors first...
        if (err) {
            // handle the error somehow...
            return;
        }

        // this is where you do any further processing...
        // for example:

        if (users[0].lastName === 'smith') { ... }
    } );
}
Acquaint answered 27/1, 2015 at 20:12 Comment(3)
Nice, your answer provided a possible explanation for using model.find() inside a loop. It's still a bad idea IMHO.Gowen
Still asynchronous.Fransen
@Fransen The query is, but not the processing of the results. That's what the OP was asking about.Acquaint
F
0
async myFunction(){
  for(var i = 0; i < n; i++) {
    var u = await User.find( {where { name: 'john'}});
  }
}
Fransen answered 7/4, 2020 at 18:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.