1

How to return from the inner callback, in the below scenario, a json data is being return, when i try to do console.log it print the [Function] instead of json value

exports.tests = function(tagName, out) {

    model.findbyTag(tagName, function(data) {

        var json = {
            "name" : 'java',
            "data" : "SomeData"
        }

        return json;

    });

}

console.log(this.tests)

it output

[Function]

What wrong i'm doing so that when this method execute it should return the json data

5
  • you can't return data from a callback Commented Oct 30, 2014 at 7:59
  • is there any way to log the json when finshed processing Commented Oct 30, 2014 at 8:01
  • sure, simply replace "return json" with "console.log(json)" Commented Oct 30, 2014 at 8:02
  • from the calling function, other module will use this data Commented Oct 30, 2014 at 8:03
  • no it won't, unless you give the other module to the callback. in async, you need to take the action to the data instead of the traditional approach of taking the data to the action. Commented Oct 30, 2014 at 8:04

3 Answers 3

3
module.exports = function() {

    var _return  = {};

    _return.someName = function(tagName ,callback){
        model.findbyTag(tagName, function(err ,data) {
            var json = {
                "name" : 'java',
                "data" : "SomeData"
            }
            callback(json);
        });
    }

    return _return ;
}

You can use above code like this in another file :

var sample_file = require('above code file address');

sample_file.someName(someTagName , function (data) {
    console.log(data) // this data is the json data
})
Sign up to request clarification or add additional context in comments.

2 Comments

That code worked perfectly please @dandavis why do you said that??
@Daniel.V: because i was talking about the code before it was edited and fixed: stackoverflow.com/revisions/26647812/1
1

You can't return data from a callback, instead you should pass a function into the method that can be called inside the callback with the result.

Example :

exports.tests = function(tagName, out, returnFunction) {

    model.findbyTag(tagName, function(data) {

        var json = {
            "name" : 'java',
            "data" : "SomeData"
        }
        // Call the returnFunction instead of trying to return data
        returnFunction(json);

    });

}

And then call it as so :

this.tests('tagName', 'out', function(r) {
    // Where "r" is the result of the callback
    console.log(r);
});

Comments

-1

You need to use Node emitter for that, if you use Node in the first place of course.

const EventEmitter = require('events');

class MyEmitter extends EventEmitter {}

const myEmitter = new MyEmitter();
myEmitter.on('event', (someProps) => {
  console.log('an event occurred!');
});
myEmitter.emit('event', {...someProps});

Then you can access the json and call any further actions that is required. Working with events might make you to restructure your application slightly. https://nodejs.org/docs/latest/api/events.html#events_emitter_on_eventname_listener

1 Comment

A link to a solution is welcome, but please ensure your answer is useful without it: add context around the link so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. Answers that are little more than a link may be deleted.

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.