Cyrille Con Morales
Cyrille Con Morales

Reputation: 957

how to return values in a function nodeJS

I was doing or making a function that will have a MYSQL result.

Now i'm making my self difficult to get those values inside the function

This is my code

var data = function () {
var expected;
db.query('Select Distinct REPLACE(title," ","-")title from antique_profile_definitions', function(err, antiqueProfile) {
    expected= antiqueProfile;
});
return expected;  
}

When I try to console.log(data) it gives me [Function: data]

In normal using POST it gives me a data which si correct

[
RowDataPacket { title: 'Land-Classification' },
RowDataPacket { title: 'Forest-Cover' }
]

Am i doing wrong already in the first place about my code?

Well I got it this will do the trick

 data(function (expected){
    db.query("Select * from antique_profile_definitions where title='" + replace + "'", function(err, result) {
        response.render('Profile',{title:replace,definition:result,antiqueProfile:expected});
    });
});

Upvotes: 0

Views: 851

Answers (3)

K.Muthu
K.Muthu

Reputation: 1252

use callback , for Example

    data(function (expected){
        console.log(expected);
    });


   function data(callback) {
       db.query('Select Distinct REPLACE(title," ","-")title from antique_profile_definitions', function(err, antiqueProfile) {
            return callback(antiqueProfile);
       });
    }

Upvotes: 1

M Salah
M Salah

Reputation: 11

query is an asynchronous method so you need a Promise to handle this.

I think this might help.

     var data = function (val) {
            // Promise which returns only after query run
            return new Promise(function(resolve,reject){
                try{
                    db.query('Select Distinct REPLACE(title," ","-")title from antique_profile_definitions', function(err, antiqueProfile) {
                        resolve(antiqueProfile);
                    });
                }catch{
                    // Returns null if anything goes wrong
                    resolve(null);
                }
                
            });
        }
        // await can be only called from a async method
        async function getdata(val){
            // call the method using an await so it wait for the promise to return
            var res = await data(val);
        }

Upvotes: 1

Mudit Kapoor
Mudit Kapoor

Reputation: 31

The first mistake here is that you have created a function with the name data and never actually called it. This is why when you log data you get this [Function: data]

But you are making another mistake that might be harder to figure out if you are not used to working with callbacks.

If you actually call your function you will end up getting undefined as the return value.


function getData() {
    var expected;
    db.query('Select Distinct REPLACE(title," ","-")title from antique_profile_definitions', function(err, antiqueProfile) {
        expected = antiqueProfile;
    });
    return expected;  
}

var data = getData();
console.log(data); // undefined


This is because you are setting the value of expected in the callback to db.query which is an asynchronous function. The answers in this question should provide a more comprehensive explanation.

Upvotes: 2

Related Questions