1

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.

3
  • 1
    Possible duplicate of How do I return the response from an asynchronous call? Commented Jul 1, 2017 at 10:15
  • Why do you implement a callback if youre not using it?? Small hint rows contains what you want Commented Jul 1, 2017 at 10:16
  • Basically, I want to return database all values which are stored in "Rows" variable. but it is do not return any value when I used to return rows Commented Jul 1, 2017 at 10:29

1 Answer 1

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

I don't think userData will get right data in async function, there is no await, so you can try to get data in callback.

getExternalLocation(args, function(err, rows) {
   var userData = rows;
   args.content = args.content + eejs.require(
    'ep_resources/templates/editbarButtons.ejs', {
     userData: userData
   });
});
Sign up to request clarification or add additional context in comments.

1 Comment

The answer is a sample code of using callback, if you want to use async/await, you need to read ES6 api about promise and async.

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.