1

Been stuck on this problem for hours. The below code:

router.route("/contact")
    .get(function(req,res){
        var response = {};
        Contact.find({},function(err,data){
            if(err) {
                response = {"error" : "Error fetching data"};
            } else {
                response = {"message" : data};
            }
            res.json(response);
        });
    })

Above query produces result with all the contacts in the database but

router.route("/contact/department/:dept")
    .get(function(req,res){
        var response = {};
        var arrDept = req.params.dept.split(",");
        if(arrDept.length == 0){
            response = {"error": " Please enter Department keywords"};
        }
        else{
            response = {};
            arrDept.forEach(function(currentValue){
                Video.find({dept: '/'+currentValue+'/i'}, function(err, data){
                    if(err){
                        response[currentValue] = "No data found";
                    }else{
                        response[currentValue] = data;
                    }
                });
            });
        }
        res.json(response);
    });

this code does not produce any output.

The code is entering the forEach loop in the else block but the query is not generating any result even if I modify the query to a basic one like

Video.find({},function(err, data){
                    if(err){
                        response[currentValue] = "No data found";
                    }else{
                        response[currentValue] = data;
                    }
                });

The response JSON is still returned blank.

PS: Problem has been simplified as is only an example of actual problem i am facing in the code. update after answer found.

2 Answers 2

2
res.json(response)

was written outside the query that is why a blank json was getting returned. The above scenario can be solved using promise as follows:

var promise = Contact.find({ dept: { $in: arrayDept }}).exec(); 
promise.then(function(resultJson) { res.json(resultJson); });

This can be used to execute all the queries in the array and return the complete json.

Sign up to request clarification or add additional context in comments.

Comments

1

Your res.json(response); in the non-working example is outside the callback of your MongoDB query, that is you are writing the response before the query's callback was actually executed and therefore your result is empty.

3 Comments

thanks DAXaholic .... but how do i append data from each iteration of the loop to the response json object ?
To do it without any further helper library you could e.g. maintain an array outside the callback with the results and add to it in the callback and as soon as the size of that array is equal to your 'arrDept' you know you have got all results. But to be honest, it is much easier and straightforward to use something like async.js. See github.com/caolan/async#map for a quick example of what I mean.
thnx... i finally solved it with promise. var promise = Contact.find({ dept: { $in: arrayDept }}).exec(); promise.then(function(resultJson) { res.json(resultJson); });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.