2

//This is my models

    module.exports.show_deatils=function(req,res,callback){

      var resultArray=[];
      mongo.connect(url,function(err,db){
        assert.equal(null,err);
        var cursor=db.collection('users').find();
        cursor.forEach(function(doc,err){
          assert.equal(null,err);
          resultArray.push(doc);
          console.log("came inside the function")
           return resultArray;


        });

    });

    }

//This is my routes

 router.get('/restful', function(req, res){
console.log("before");
  User.show_deatils(function(req,res){
console.log(resultArray);
    req.session.resultArray=resultArray;


  });

    res.render('restful',{items:req.session.resultArray});
 });

//Here I am calling a function from routes to models (show_details).The issue which I am facing is I am calling the function.The method is being called.The array "resultArray" is been populated with the values.But I am not been able to return the particular value.How can we do it?

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
riza
  • 123
  • 2
  • 11
  • You need to read and understand the answer in the QA I linked to. There's now way around understanding this basis of the language if you want to code in Javascript. – Denys Séguret Feb 28 '17 at 07:01

2 Answers2

1

Try this:

//The model

I added the call to the callback:

module.exports.show_deatils=function(req,res,callback){

  var resultArray=[];
  mongo.connect(url,function(err,db){
      assert.equal(null,err);
      var cursor=db.collection('users').find();
      cursor.forEach(function(doc,err){
          assert.equal(null,err);
          resultArray.push(doc);
          console.log("came inside the function")
          return resultArray;
    });

    //now call the callback <----
    callback(resultArray);

});

}

//The routes

The User.show_details has to take 3 params, the last one is the callback to execute:

 router.get('/restful', function(req, res){
    console.log("before");
    User.show_deatils(req, res, function(resultArray){
            console.log(resultArray);
            req.session.resultArray=resultArray;
            res.render('restful',{items:req.session.resultArray});
        });
 });
mvermand
  • 5,829
  • 7
  • 48
  • 74
0

Use the callback function to return the data.

module.exports.show_deatils=function(req,res,callback){
      var resultArray=[];
      mongo.connect(url,function(err,db){
        assert.equal(null,err);
        var cursor=db.collection('users').find();
        cursor.forEach(function(doc,err){
          assert.equal(null,err);
          resultArray.push(doc);
          console.log("came inside the function")

        });  
        callback(null, resultArray);  
    });    
    }

=======================

router.get('/restful', function(req, res){
console.log("before");
  User.show_deatils(req, res, function(err, resultArray){
   if(!err) {
    console.log(resultArray);
    req.session.resultArray=resultArray;
   }
  });    
    res.render('restful',{items:req.session.resultArray});
 });

You can also polish the error logic to make it robust.

Rudra
  • 1,678
  • 16
  • 29