javascript - How to pass data from Server to Client in Meteor -


i learning meteor , javascript. using npm package meta data of url on server side. works fine. undefined when passing result client. appreciate help.

here code

if (meteor.isclient) {     meteor.call('getmetadata', "http://www.bbc.co.uk/news", function (err, data) {         if (err) {             console.log("error", err);         };         console.log("meta data: " + data);  //shows undefined     }); }   if (meteor.isserver) {     meteor.startup(function () {         var preview = meteor.npmrequire('page-previewer');         meteor.methods({             getmetadata: function (url) {                 preview(url, function (err, data) {                     if (!err) {                         console.log(data);  //works fine                         return data;                     }                 });             }         })     }); } 

you need convert preview function synchronous function,using future this, make function wait normal err,data callbacks synchronous function.

var future = npm.require('fibers/future'),   preview = meteor.npmrequire('page-previewer'); meteor.methods({   getmetadata: function(url) {     var f = new future();     preview(url, function(err, data) {       if (!err) {         return f.return(data);       }     });     return f.wait();   } }); 

now snippet should work

if (meteor.isclient) {     meteor.call('getmetadata', "http://www.bbc.co.uk/news", function (err, data) {         if (err) {             console.log("error", err);         }else{              console.log("meta data: " + data);  //shows undefined         }     }); }; 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -