admin管理员组文章数量:1279043
function add(post)
{
var word = new KeyWord({ keyword: post.keyword});
word.save(function (err, word)
{
if(err)
{
if(err.code==11000)
return post.keyword + ' is already added.';
}
else
return 'Added : ' + post.keyword;
});
}
When I am trying to read return value of add function it returns nothing.
And also when I am trying to put message in variable and return that from outside also give null value.
function add(post)
{
var word = new KeyWord({ keyword: post.keyword});
word.save(function (err, word)
{
if(err)
{
if(err.code==11000)
return post.keyword + ' is already added.';
}
else
return 'Added : ' + post.keyword;
});
}
When I am trying to read return value of add function it returns nothing.
And also when I am trying to put message in variable and return that from outside also give null value.
1 Answer
Reset to default 9To put it simply, you can't. To get values from functions like these, you must use a callback:
function add(post, callback) {
var word = new KeyWord({keyword: post.keyword});
word.save(function(err, word) {
if (err) {
if (err.code==11000) callback(post.keyword + ' is already added.');
else callback('Added : ' + post.keyword);
}
});
}
You'd then use the function like this:
add(post, function(result) {
// return value is here
}
本文标签: javascriptHow can we return string from callback function to root function in nodejsStack Overflow
版权声明:本文标题:javascript - How can we return string from callback function to root function in node.js? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741222554a2361242.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论