How can I use findOne method in meteor?

- QUESTION -

Firstly, I am new in Meteor and JS. I am trying to learn "findOne". I created a collection. Its name is "Rezervasyon" and there is a field "rezervasyonnumarasi" in this collection. I want to make this, if record exist, write console:"yes" otherwise "no". I am sharing my code. What is wrong with my code?
rezervasyon.js
 Template.content.events({
'click #rezervasyonsorgula': function(e, template) {
 var sorgu = template.$('#rezno').val();
 result = Rezervasyon.findOne({rezervasyonnumarasi:sorgu});
 if (result)
  {
  console.log("yes");
  }
 else {
  console.log("no");
      }

 }
});
content.html
<p id='sorgusonuc'></p>
 <form class="form-vertical pull-right">
      <div class="form-group">
       <span class="f-s-20 text-black">Rezervazyon         Sorgula</span>
       </div>
       <div class="form-group">
         <input name="bulten" type="text" class="form-control 
          width-250 m-  auto" id="rezno" placeholder="Rezervasyon Kodunuz">
       </div>
       <div class="form-group">
          <button type="button" class="btn btn-theme width-250" id="rezervasyonsorgula" >Sorgula</button>
        </div>
   </form>
Thank you for all helps.

- ANSWER -

Are you sure that your HTML code is encapsulate in a template element ?
 <template name="content">
     <p id='sorgusonuc'></p>
     <form class="form-vertical pull-right">
     ...
 </template> 
With findOne, you can :
Rezervasyon.findOne(); // Return a random element
Rezervasyon.findOne(_byId); // Search by mongo _id
var option = {param: value};
Rezervasyon.findOne(option); // Search with a more complex query
findOne return "undefined" or an object
You can also use find() and iterate over the returned array, which can be empty or with elements inside.
Hope it will help you.

0 comments:

Post a Comment

Powered by Blogger.