Skip to content

Instantly share code, notes, and snippets.

@stephentcannon
Created May 26, 2012 10:01
Show Gist options
  • Save stephentcannon/2793220 to your computer and use it in GitHub Desktop.
Save stephentcannon/2793220 to your computer and use it in GitHub Desktop.
findOne template view issue
//all of this is client side
//some of the concept is taken from
//http://stackoverflow.com/questions/10167464/meteor-rendering-template-with-a-document-from-a-collection/10167515#comment13990810_10167515
//collection setup
myCollection = new Meteor.Collection('myCollection');
//subscription is working
//the entire contents render perfectly in a template that shows all records
//it shows changes made server side or client side
//here is the subscription line and it is working
Meteor.subscribe("myCollection", Session.get('userID'));
//there is a second template to show results based on a keyword
//user types key word in form, key word does a findeOne on subscribed collection
//this template code does indeed output a nice mongo record based on the search keyword
//just can't figure out how to format in the html/hs template mark up
//but here is the template code being called
Template.myTemplate.getData = function(){
var obj = myObj.getMyData(Session.get("searchValue"));
console.log('obj: ' + JSON.stringify(obj));
return obj;
}
//code being called by template code
//this is not in the template code because it is called by other functions (non template based) that need to work with a single record, those other functions are able to easily work with a single record by name
//reasoning: code duplication reduction
//it returns a single record that looks like this
//{"_id":"e2ede046-3743-42d1-855e-ab8d91113bf3","created":"2012-05-26T09:32:44.995Z","updated":"2012-05-26T09:32:44.995Z","user_id":"5ef4b712-1ef4-45c0-8e76-133ccfd47ea0","name":"bob","value":"bobby"}
//here is the method called by the Template code and by other functions/methods
myObj.getMyData = function(name){
var item = myCollection.findOne({name: name});
return item; // && item.name; //removed && item.name because it messes up other code but works for template code if I add it back in
}
//template mark up, where I run into trouble
//all that outputs on the browser screen is [object Object]
<template name="myTemplate">
{{#each getData}}
{{name}} or {{this.name}}
{{/each}}
</div>
</template>
@stephentcannon
Copy link
Author

{{#getData}}
{{name}} or {{this.name}}
{{/getData}}

returning
return obj; //outputs [object Object]

return JSON.stringify(obj); //outputs the raw json record data

@stephentcannon
Copy link
Author

this solved it

{{#with getData}}
{{#if getData}}
{{ name }}
{{/if}}
{{/with}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment