2

I want muliple values in my callback function

myfunction(i,function(err,result){
  console.log(JSON.parse(JSON.strigify(result)));   
 ****//i need value here..****


});
function myfunction(i,callback) {
/* my some stuff */
var callBackString= new Array();
                        callBackString['value1']=value1;
                        callBackString['value2']= value2;
                        callBackString['value3']= value3;
                        callback(null,callBackString);

};

I am returting callBackString as an array..,

Can anybody tell me how to do that

Thanks

4
  • And what is the problem? Commented Nov 3, 2014 at 9:31
  • I am not able to get returned values Commented Nov 3, 2014 at 9:33
  • 2
    Try to use var callBackString = {}. You are using your array like an object, so make it one. When you serialize the array with JSON.stringify() those non-array properties get lost. Commented Nov 3, 2014 at 9:35
  • Typo at stringify? You omitted an n there. Commented Nov 3, 2014 at 10:23

3 Answers 3

7

You should use an object instead:

function myfunction(i, callback) {
  var callBackString = {};
  callBackString.value1 = value1;
  callBackString.value2 = value2;
  callBackString.value3 = value3;
  callback(null, callBackString);
}

Then the receiving end:

myfunction(i, function(err, result) {
  var value1 = result.value1;
  var value2 = result.value2;
  var value3 = result.value3;

  console.log(JSON.parse(JSON.strigify(result)));   
});
Sign up to request clarification or add additional context in comments.

1 Comment

The last line in the callback is unnecessary. If you want to make a copy of the data, it should be the first there.
2

The following should work:

function myfunction(i, callback) {
  // ...
  // Returns an Array
  return callback(null, [value1, value2, value3])
}

function myfunction2(i, callback) {
  // ...
  // Returns an Object
  return callback(null, {value1: value1, value2: value2, value3: value3})
}

Comments

-1

Try this

Hope it works

myfunction(i,function(err,value1,value2,value3){
  console.log(JSON.parse(JSON.strigify(result)));   
 ****//i need value here..****


});


function myfunction(i,callback) {
/* my some stuff */
var callBackString= new Array();
                        callBackString['value1']=value1;
                        callBackString['value2']= value2;
                        callBackString['value3']= value3;
                        callback(null,value1,value2,value3);

};

Comments

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.