1

I have written below code in one file:

models/exported.js

 module.exports = {
        processedList: function(store_name) {
            var t;
           var tradeIds = exported.find({storename: store_name}, function (err, value) {
                if (err) return console.error(err);
             return value;

            }).select('tid -_id');

        }, // Export connection here
    };

I have another file in routes

routes/exported.js

var exported = require('../models/exported.js');
var tradeIds = exported.processedList(storename);
    console.log('simer'+tradeIds);
}

but I get undefined in console.log. If instead of return statement in processedlist I write console.log then the result gets console. But my requirement is to return data from model file to route file.

I am new to express and node js.

I guidance would be highly appreciated.

2 Answers 2

1

Acoording to your question, you want calling a function from route and get return response from your function to route. simple use callback functions.

models/exported.js

 module.exports = {
    processedList: function (store_name, callback) {
        var t;
        var tradeIds = exported.find({storename: store_name}, function (err, value) {
            if (err) {
                callback("error", err)
            } else {
                callback("success", value)
            }
        }).select('tid -_id');
    }
}

routes/exported.js

  var exported = require('../models/exported.js');
exported.processedList('storename', function (err, results) {
    if (err == 'error') {
        console.log(err);
    } else {
        console.log(results);
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Simer i am just updated my code hope this works for you. last time there may be syntex error. and still any issue let me know.
Both the solutions are working, yours and kawadhiya21, But it means if I put routes/exported.js code in some function and want to call that funtion then I have to make a call back for that too.Every time we want to call another function we need to use call backs.
1

You are trying sync operation in async environment. processedList may or may not have completed when you try to console log tradeIds. NodeJS would not wait for it to complete because it is asynchronous in nature (by design and it is not a bug). You can pass callback rather than executing this way.

models/exported.js

module.exports = {
        processedList: function(store_name, cb) {
            var t;
           var tradeIds = exported.find({storename: store_name}, function (err, value) {
                if (err) return cb(err);
             cb(null, value);
        }).select('tid -_id');

    }, // Export connection here
};

routes/exported.js

var exported = require('../models/exported.js');
exported.processedList(storename, function(err, results) {
    if (err) { console.log(err); }
    console.log(results);
});

This makes sure that console.log happens only when processedList finishes execution.

2 Comments

It is working. But it means if I put routes/exported.js code in some function and what to call that then I have to make a call back for that too.Every time we have to call another function we need to use call backs.
Yes it works that way. All of javascript is asynchronous. And callback is one of the ways to channelize your tasks.

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.