admin管理员组文章数量:1415421
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
3 Answers
Reset to default 3module.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
本文标签: javascriptHow to return data from the callback nodejsStack Overflow
版权声明:本文标题:javascript - How to return data from the callback nodejs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745236723a2649082.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论