質問

I've got this in my client.js file

Template.data.champ = Meteor.call("checkLeague", function(error, results) {
        console.log(results.data.data);
        return results.data.data;
});

So it shows fine in the console.log but it doesn't actually show on the webpage.

This is my html file with handlebars template

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

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

<template name="data">
    {{#each champ}}
        {{name}}
    {{/each}}
</template>

From my understanding (which is very limited in terms of Handlebars) but the {{#each champ}} iterates over objects? But for some reason, nothing is being displayed on the page.

This is the object structure (displayed in the console).

Object {Aatrox: Object, Ahri: Object, Akali: Object, Alistar: Object, Amumu: Object…}
Aatrox: Object
   id: "Aatrox"
   image: Object
   key: "266"
   name: "Aatrox"
   title: "the Darkin Blade"
   __proto__: Object
Ahri: Object
Akali: Object
Alistar: Object
Amumu: Object
Anivia: Object
Annie: Object
Ashe: Object

So basically I am passing an object that has properties which have values of objects. I assume the {{#each} iterates over the properties and gives access to the values (which is an object) and then I try to access the name property of that variable in the handlebars template but it doesn't work.

役に立ちましたか?

解決 2

Peppe's answer is correct - here is an option for how to deal with this situation:

Template.data.created = function() {
  Meteor.call('checkLeague', function(error, results) {
    Session.set('champ', results.data.data);
  });
};

Template.data.champ = function() {
  return Session.get('champ');
};

The data is loaded when the template is created, and asynchronously stored into a session variable. Keep in mind that this isn't reactive, but that's hard to overcome since your data is coming from a method call.

他のヒント

From the docs regarding Meteor.call:

If you include a callback function as the last argument (which can't be an argument to the method, since functions aren't serializable), the method will run asynchronously: it will return nothing in particular and will not throw an exception.

So whatever value Template.data.champ is assigned, it's "nothing in particular" (note that what you return from the callback you have will never be used anywhere).

You could store it in a Session like this:

Session.setDefault('theData', [])

Meteor.call("checkLeague", function(error, results) {
    Session.set('theData', results.data.data)
});

Template.data.champ = function(){
    return Session.get('theData')
}

But I would try to go with a collection instead.

I think the problem might just be that #each is expecting an array and you are passing an object. In your helper, try return _.toArray( result.data.data );.

I couldn't find any mention of this in the meteor docs but handlebars.js docs mentions Array. Also, I have passed Array before and it works.

In addition to what was said about using a session variable. I think you will also need to transform your list into a list of just objects instead of a list of key, value you pairs.

So instead you want to make your data just like this

var champ = [{ id: "Aatrox",
   image: Object,
   key: "266",
   name: "Aatrox",
   title: "the Darkin Blade"},   
   { id: "Ahri",
   image: Object,
   key: "267",
   name: "Ahri",
   title: "Hitchhikers Guide"}, ... ];

return champ;

To get this structure from your current structure you'll need to do something like

var champ = [];

for (var a in results.data.data) {
  for (var key in results.data.data[a]) {
      champ.push(results.data.data[a][key]);
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top