mongodb - Meteorjs method call return undefined even though having callback -


i'm playing meteorjs , had hard time trying figure out happen async method written.

//in methods.js feedbacktag = new meteor.collection('feedbacktag');  meteor.methods({   searchtag: function (tag, collections) {     var result;     if(collections.tolowercase() == 'feedback')     {     result = feedbacktag.find({tag: tag});     }     return result;   } });  //in client.js   template.executefb.events({ 'keyup input#searchfeedback':    function(e) {      if(e.which == '13')     {       var tag = $('#searchfeedback').val();       meteor.call('searchtag', tag, 'feedback', function(err, data){         //err returns:internal server error, data returns undefined         console.log(err, data)        });     }   } }); 

i had no idea why return internal server error:500. advice please.

thanks in advance!

update:

i realised results becomes 'undefined' when called in client side. however, if called directly client i.e.

var result = feedbacktag.find({tag: tag}); 

it returns me data want.

any ideas how results methods class instead? thanks

try adding .fetch() collection call on server. return actual array of data, otherwise you're returning cursor, in meteor.publish().

this might what's causing error.

    meteor.methods({       searchtag: function (tag, collections) {         if(match.test(tag, string) && match.test(collections, string) {           if(collections.tolowercase() === 'feedback') {             return feedbacktag.find({tag: tag}).fetch();           } else {             console.log("should have sent feedback, sent " + collections);           }          } else {           throw new meteor.error(400, "invalid inputs!");       }     }); 

i've modified code bit, because wise start throwing own errors, , wise use meteor's new match package validate inputs.

methods return either error object or response object. you'll have condition on receiving end, instead of trying show both did console.log.

function(err, res) {    if(!!err) {      alert(err.reason); /* or console.log(err) */   } else {     console.log(res);   } } 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -