pyprism
pyprism

Reputation: 3008

Passing data from server block to client block and also calling 3rd party package in Meteor

I am not able to pass data from server block to client block . I am also not sure the way I call self created package is right or wrong . Here is my folder structure :

x/packages/me.js
x/packages/package.js
x/packages/smart.json
x/x.css
x/x.html
x/x.js 

Here is all code :

me.js

Meteor.methods({
getTweets: function () {
var key = "my api key";
var response = Meteor.http.call( "POST", "http://localhost:8000/users.json",
{ params: { appkey: key ,uid: "example" }});
return response.content;
}
});

package.js

Package.describe({
summary: "summary etc"
});

Package.on_use(function (api){
api.use(["http"],["server"]);

api.add_files("me.js","server");
});

smart.json

{
"name": "name example",
"description": "desc",
"homepage":"as",
"author" : "xyz",
"version" : "0.1",
"packages": {}
}

x.html

<head>
<title>x</title>
</head>

<body>
{{> hello}}
</body>

<template name="hello">
<h1>Hello World!</h1>
{{greeting}}
<input type="button" value="Click" />
</template>

x.js

if (Meteor.isClient) {
Template.hello.greeting = function () {
return "Welcome to y.";
};

Template.hello.events({
'click input' : function () {
// template data, if any, is available in 'this'
Meteor.call("hiren",function (err, data) {
console.log(data);
if(err) throw err;
});
}
});
}

if (Meteor.isServer) {
Meteor.call("getTweets",function (err, data) {
Meteor.methods({
"hiren":function(){
return data;
}
});
});
}

When I click the "click" button , it just shows undefined

Upvotes: 0

Views: 293

Answers (1)

Hubert OG
Hubert OG

Reputation: 19544

One problem is here:

if (Meteor.isServer) {
  Meteor.call("getTweets",function (err, data) {
    Meteor.methods({
      "hiren": function(){
        return data;
      },
    });
  });
}

You define your methods inside a callback function of getTweets method. This is probably not what you want to do, usually those are defined right in the server block as you want to have a predictable API. I cannot think of a reason to do such thing as you did, but even if there is one - you didn't call the getTweets function. So I guess it's a mistake.

Upvotes: 1

Related Questions