1

First off, I want to apologize if this has been asked before. I've spent the last 5 hours (yes, 5 hours) trying to get this code to work.

I have the feeling I should be using promises, but I need your guys advice. If I don't need to use a promise, how can I get what I have working?

I've created a function to pull data from MongoDB:

var getData = function(db, channel, callback) {
      var cursor = db.collection('streams').find({'channel': channel});
      cursor.toArray(function(err, doc) {
         assert.equal(err, null);
         if (doc == null) {
             console.log("No docs found");
         } else {
             doc = doc[0];
             callback(null, doc);
         }
      });
    };

I'm able to console log the data using this function.

getData(db, "#cikez", function(err, result){
        assert.equal(err, null);
        if (result == null) {
            console.log("didn't work");
        } else {
            console.log(result); <-- logs results perfectly
        }
    });

What I would like to do is put the above function into a variable and return the data (not console log it).

var finalData = getData(db, "#cikez", function(err, result){
            assert.equal(err, null);
            if (result == null) {
                console.log("didn't work");
            } else {
                console.log(result); <-- I want to return this data.
            }
        });

So I can use it like this.

client.raw("PRIVMSG #jtv :/w " + user.username + " " + randomResponse + " " + finalData); 
Jayson H
  • 1,928
  • 4
  • 21
  • 28

0 Answers0