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.
To 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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With