Riya Singhal Riya Singhal - 6 months ago 57
Javascript Question

how can i get the fetched record in route model node js

I am getting the fetch records on console, but I want to access on route page.

module.exports.getcoin_info = function(err,callback){
var results = [];

coin_model.find(function(err,teams){
if(err){
throw err;
}

results.push(teams);

console.log(results);
});
}


route page in this function I am not getting records. Please guide me how it will proceed if there are any another way for the same please guide me.

monogoose_user.getcoin_info(function(err, results) {
if (err) {
console.log('here');
} else {
console.log(results);
}
});

Answer Source

Why not do the following?

module.exports.getcoin_info = function(callback) {
    coin_model.find(function(err, teams) {
        callback(err, teams);
    });
}

This way you will be able to access both the error and teams in your callback.

Basically you are just calling the callback function with 2 parameters (err and teams) after it's done searching the DB.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download