0

I have return the function in mysql.js as :

function myFunction(resourceIdentifiers,callback){

    dbconnection.execute( function(err,response) {

        response.query('call SP_ExposePricingDetailforUI(' 
                                      + resourceIdentifiers + ')'
                      ,function (err, rows, fields) {
                          console.log(rows);
                      });
                 }
        );
        return rows;            
}

And tried to call it at another script file restservice.js as :

mysql.myFunction(resourceIdentifiers , function(err,rows) {
    console.log(rows);
}

But I get error as the function myFunction is Undefined.

2
  • Are you exporting myFunction? i.e.: module.exports.myFunction = myFunction; Commented Jul 22, 2013 at 21:38
  • actually i need the result to be sent to api call as response from restservice.js and not from mysql.js. kindly suggest me a solution Commented Jul 22, 2013 at 21:40

2 Answers 2

1

If mysql.myFunction is undefined, then you're probably not actually exporting it:

function myFunction(resourceIdentifiers, callback){
    // ...
}

exports.myFunction = myFunction;

Function and variable declarations are "private" to the module by default. Only those members you explicitly export will be accessible from other modules.


You also won't be able to use return rows; as you're trying to. Asynchronous code is event driven and doesn't wait, which return would need it to do.

myFunction already has a callback argument and you're passing a function for the value. You just need to call it:

// ...
function (err, rows, fields) {
    callback(err, rows);
}
// ...

You should also at least escape resourceIdentifiers when concatenating.

But, generally better is to use a placeholder (?) and the optional, 2nd argument to .query():

response.query(
    'call SP_ExposePricingDetailforUI(?)',
    [ resourceIdentifiers ],
    function (err, rows, fields) {
        callback(err, rows);
    }
);
Sign up to request clarification or add additional context in comments.

Comments

0

You just need to callback within the result of response.query. Something like this.

mysql.js:

function myFunction(resourceIdentifiers,callback){

    dbconnection.execute( function(err,response) {

        response.query('call SP_ExposePricingDetailforUI(' 
                                      + resourceIdentifiers + ')'
                      ,function (err, rows, fields) {
                          callback(err, { rows: rows, fields: fields});
                      });
                 }
        );
}

module.exports.myFunction = myFunction;

restservice.js:

mysql.myFunction(resourceIdentifiers , function(err,resp) {
    console.log(resp.rows);
}

Update - removed the return rows statement that I missed the first time around.

3 Comments

thanks dc5. but when i run above script , i get rows is not defined
Try console.log(rows) from within your response.query callback. What is logged there?
@Prem - I'd inadvertently left the return rows statement in the code. Does that get rid of your rows not defined error?

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.