1

I'm trying to transfer 2 results from 2 callback to 1 lodash function (_.union) using recursive function. I dont understand what am I doing wrong! I keep getting "undefined". Here is my code:

  • EDITED:

My new code with the "promise" technique

the first function that check things in the remote DB-

function findFiles(kw, callback){
    if (_.isArray(kw)) {return callback(kw)};

    return new Promise((resolve, reject) => {
        console.log(kw);
        word.aggregate([
                     { $match: { keyWord: kw } },
                     { $project: { filesFound: '$filesFound.fileName' , '_id':0} },
                     { $sort: { fileName: 1 } }
                   ],function(err, obj){
                    console.log('checked' + kw);
                    console.log(obj);
            if (err) return reject(err);
            else      
                return resolve(obj[0].filesFound);//obj[0].filesFound
        })
    })
}

The main function:

    function searchOperation(query, callback){
    var stack=[];
    if (!(_.includes(query, 'OR')) && !(_.includes(query, 'AND')) && !(_.includes(query, 'NOT'))){

        findFiles(query)
        .then((item) => {
            console.log(item+'********');   
           callback(item)
        })
        .catch((err) => {
          console.log(err)
        })
    }
    else{
        wordsArr = _.split(query, " ");
        console.log("+++++:" + wordsArr);
        wordsArr.forEach(function(w){
            console.log('first check:'+w);

            if(_.isEmpty(stack)){

                if(_.includes(w, 'OR')){
                    console.log('found OR');
                    var statement = [];
                    console.log('query is:'+query);
                    statement = _.split(query, w, 2);
                    console.log(statement[0]+' , '+statement[1]);
                    return new Promise((resolve, reject)=>{


    resolve(_.union(searchOperation(statement[0]),searchOperation(statement[1])))
                        })
//ANOTHER OPTION:
                        // searchOperation(statement[0],function(arr1){
                        //     console.log('arr1');
                        //     console.log('done part 1!');
                        //     searchOperation(statement[1],function(arr2){
                        //         console.log('who called arr2?');
                        //         return(_.union(arr1,arr2));
                        //     })
                        // });
                    }
                }
            })
        }
    }

now, inside the function findFile() the console.log return what i need. but then I need to use both returned values in another function (union) and it returns undefined.

in the main thread:

searchOperation('Expression1 OR Expression2', function(result){
    res.json(result);
})

now i'm sure: something goes WRONG in the recursive function and the async of node...

I need it to work recursively and could get complicate expressions like:

'((A NOT B) AND (C OR D))' 

does some know what is the right way to write it either with Promise or async.waterfall ?? thanks in advance!!!

DavidA
  • 608
  • 8
  • 9
  • 2
    Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – gyre Mar 05 '17 at 08:43

1 Answers1

1

Your code doesn't work because you're trying to get an Async response in a Synchronous fashion.

Look into Promises.

Nijikokun
  • 1,514
  • 1
  • 15
  • 22
  • do you know what is the right way to write it with async.waterfall? – DavidA Mar 05 '17 at 10:54
  • You would pass a callback or use a promise in your `query` function and invoke the callback upon completion of the waterfall callbacks basically resolve your promise in the waterfall invocation: https://www.npmjs.com/package/async-waterfall#tasks-as-array-of-functions – Nijikokun Mar 05 '17 at 11:46
  • thank you, I saw it already. still don't know how to combine it with my code... :( – DavidA Mar 05 '17 at 12:21