With a template, how to use {{attribute}} with one "record" compared to C # each with the find () cursor?

I know that with the help of the template you can display several documents with your attributes, for example:

// html
<template name="hello">
{{#each greetings}}
   {{message}}
{{/each}}
</template>

// js
Template.hello.greetings = function() {
   return Greetings.find();
}

What Greeting.message shows for each welcome document found.

My question is how to use this template for only one document? (including accessible document)

From the javascript side, I would use something like return Greetings.findOne({'id' : Session.get("greeting_id")});

But when using the template:

<template name="hello">
   {{message}}
</template>

an error is issued: Uncaught TypeError: Unable to read property message 'undefined

UPDATE

I am currently using this on the JS side using the template suggested by @ tom-wijsman below:

Template.hello.greeting = function() {
   var greeting = Greetings.findOne({'id' : Session.get("greeting_id")})
   if (greeting)
       return greeting;
   return {message: ""};
}
+5
source share
1 answer

Handlebars.js # .

<template name="hello">
    {{#with greeting}}
        {{message}}
    {{/with greeting}}
</template>

 

Template.hello.greeting = function() {
    return Greetings.findOne({'id' : Session.get("greeting_id")});
}
+8

All Articles