最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to return data from the callback nodejs - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question asked Oct 30, 2014 at 7:58 anishanish 7,44814 gold badges84 silver badges153 bronze badges 5
  • you can't return data from a callback – dandavis Commented Oct 30, 2014 at 7:59
  • is there any way to log the json when finshed processing – anish Commented Oct 30, 2014 at 8:01
  • sure, simply replace "return json" with "console.log(json)" – dandavis Commented Oct 30, 2014 at 8:02
  • from the calling function, other module will use this data – anish 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. – dandavis Commented Oct 30, 2014 at 8:04
Add a ment  | 

3 Answers 3

Reset to default 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
})

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);
});

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/docs/latest/api/events.html#events_emitter_on_eventname_listener

发布评论

评论列表(0)

  1. 暂无评论