0

I'm trying to save find results to a variable, I looked around and couldn't find any clear answer, here is what i want to do:

var data;    
myModel.findOne({
            where: {
                Key: 'myKey'
            }
        }, function(err, model) {
            if (err) {
                console.log(err);
            }
            data = model;
        });

I looked at this similar question but didn't find an answer.

  • It doesn't make sense to store `model` in `data`. Just use model where it is. You can't return it to the outer scope in a useful way. – Kevin B Sep 06 '18 at 15:17
  • 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) – Kevin B Sep 06 '18 at 15:17

1 Answers1

1
// Here you create a new variable called data
var data;

myModel.findOne({
  where: {
    Key: 'myKey'
  }
}, function(err, model) {
  // You create a new variable called data
  // it's different from the first data
  // The keyword var here means you want to declare a new variable
  // not use an existing one
  var data = model;
});

A working example close to what you have is :

But I think that you will struggle to know when to use data and run into an asynchronism issue.

// Here you create a new variable called data
var data;

myModel.findOne({
  where: {
    Key: 'myKey'
  }
}, function(err, model) {
  // You create a new variable called data
  // it's different from the first data
  // The keyword var here means you want to declare a new variable
  // not use an existing one
  data = model;
});

Here is one of my proposal :

function getMyData(callback) {
  myModel.findOne({
    where: {
      Key: 'myKey'
    }
  }, function(err, model) {
    callback(err, model);
  });
}

getMyData(function(err, data) {
  if (err)...

    // I can use my data here
    // ...
});

Now using ES6, promises :

// findOne can be used with a callback, or it can return a Promise object
function getMyData() {
  return myModel.findOne({
      where: {
        Key: 'myKey'
      },
  });
}

getMyData()
  .then((data) => {
    // Use the data here ...
  })
  .catch((err) => {
    // Handle the error here ...
  });

EDIT: Using Promise.all to run in parallel multiple database requests

function getMyData(key) {
  return myModel.findOne({
    where: {
      Key: key,
    },
  });
}

Promise.all([
    getMyData('Key1'),
    getMyData('Key2'),
    getMyData('Key3'),
  ])
  .then((dataInArray) => {
    // Use the data here ...
    // dataInArray[0]
    // dataInArray[1]
    // dataInArray[2]
  })
  .catch((err) => {
    // Handle the error here ...
  });
Orelsanpls
  • 22,456
  • 6
  • 42
  • 69
  • Thank you, But what I want to do is save the findOne result in a variable outside findOne's scope if that makes sense. – kareemhossam Sep 06 '18 at 14:28
  • The thing is, if you do that, when are you gotta know when using the var or not ? Because the fill of the var would be asynchronous – Orelsanpls Sep 06 '18 at 14:31
  • you see, I'm gonna perform this operation 3 times to get 3 pieces of data to setup another part of the code, so it doesn't make sense to me that I have to nest 3 inside each other to perform the required operation. – kareemhossam Sep 06 '18 at 14:42
  • I've add an example where I perform 3 asynchronous requests and treat the data – Orelsanpls Sep 06 '18 at 15:01