Node Js callback return function

95 views
Skip to first unread message

Sushil Kumar Kishan

unread,
Jul 1, 2017, 8:06:46 PM7/1/17
to nodejs

I want to return database value in node js and pass as a variable in ejs file.

Bellow is the code, Which I used. it did not return value.

function getExternalLocation(cb) {  

    mssql.connect(msSqlSettings, function (err ) { 
        if (err) {
            cb(err);
        }       
        var getQuery = "SELECT [Title] FROM [dbo].[StyleTemplates] " ;
        //console.log(getQuery);
        var request = new mssql.Request();          
        // query to the database and get the data   

        request.query(getQuery, function (err, rows) {
            mssql.close();  
            cb(err, rows);          
        });
    }); 
} 

exports.eejsBlock_editbarMenuLeft = function (hook_name, args, cb) {

    var userData = getExternalLocation(args, function(err, rows) {});
    args.content = args.content + eejs.require('ep_resources/templates/editbarButtons.ejs', {userData : **userData** });
    return cb();
})

Userdata did not return any value;

Alex Wells

unread,
Jul 3, 2017, 4:24:19 PM7/3/17
to nodejs
Hi Sushil,

Why would you expect it to? Where in your function 'getExternalLocation' do you see the word "return"?

var userData = getExternalLocation(args, function(err, rows) {});

In order to move forward, you need to understand the difference between procedural and functional programming.

by default, JavaScript functions return 'void', which is what you're getting in 'userData' variable. You are however supplying a 'function' (which takes two parameters, 'err', and 'rows') but which is anonymous, to the function you're calling, 'getExternalLocation'.

Take another look at the body of getExternalLocation, the function you supply when you call it, is called 'cb', and on the line:

            cb(err, rows);          

you call the anonymous function you supplied earlier with the results of your MSSQL query. The upshot of all this is that in the function body of the anonymous function you supply to the 'getExternalLocation' function, you will have the results you need, so you're anonymous function should look like this:

    getExternalLocation(args, function(err, rows) {
            console.log(err);
            console.log(rows);
    });

at this stage, the results you require will be there. You need to try and understand how you're then going to call the callback in the parameters to the exported function here:

exports.eejsBlock_editbarMenuLeft = function (hook_name, args, cb) {

In order to understand how to get the results of your work out to the rest of your application. This is, in my understanding the hardest thing to understand about JS, so ask questions, work on it a lot, have a look at callback pyramids, the Async library, and finally try and understand promises

Promises are the de-facto way to do things now, so in a lot of ways you may as well jump straight to using those - but it's definitely good to understand the function passing style of JavaScript as a general concept.

Cheers,
Alex

Sushil Kumar Kishan

unread,
Jul 4, 2017, 1:52:09 PM7/4/17
to nodejs
HI Alex,

Is this any way to return value instead on a console.

 let getDatav=  getExternalLocation(args, function(err, rows) {
            console.log(err);
            return rows;
    });

     console.log(getDatav)

###
 getExternalLocation(args, function(err, rows) {
            console.log(err);
            console.log(rows);
    });

Naveen Kerati

unread,
Jul 5, 2017, 12:40:26 AM7/5/17
to nodejs
Hi sushil ,
why do you want to return values from a callback function . where you can pass the userdata from callback to ejs directly.

Sushil Kumar Kishan

unread,
Jul 5, 2017, 2:40:37 AM7/5/17
to nod...@googlegroups.com
Hi Naveen,

Can you give me a sample code, or please modify the below code if possible, because I am trying but did not find any success.

function getExternalLocation(cb) {  

    mssql.connect(msSqlSettings, function (err ) { 
        if (err) {
            cb(err);
        }       
        var getQuery = "SELECT [Title] FROM [dbo].[StyleTemplates] " ;
        //console.log(getQuery);
        var request = new mssql.Request();          
        // query to the database and get the data   

        request.query(getQuery, function (err, rows) {
            mssql.close();  
            cb(err, rows);          
        });
    }); 
} 

exports.eejsBlock_editbarMenuLeft = function (hook_name, args, cb) {

    var userData = getExternalLocation(args, function(err, rows) {});
    args.content = args.content + eejs.require('ep_resources/templates/editbarButtons.ejs', {userData : **userData** });
    return cb();
})

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+unsubscribe@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/1a9233d5-61bf-4e89-a10f-3fd12495f07f%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Jason Zheng

unread,
Jul 30, 2017, 3:57:54 PM7/30/17
to nodejs
 hello , try the code below :

const co =require("zco");

exports.eejsBlock_editbarMenuLeft = function (hook_name, args, cb) {
    co(function*(co_next){
    let [err,userData] = yield getExternalLocation(args, co_next);

    args.content = args.content + eejs.require('ep_resources/templates/editbarButtons.ejs', {userData : **userData** });

    return cb();
    })()
})
Reply all
Reply to author
Forward
0 new messages