Meteor Blaze helper 和 Mongo collection.find with Session key selector

Meteor Blaze helper and Mongo collection.find with Session key selector

此 Meteor 代码使用 Meteor Blaze 模板助手,它为一些 html 元素提供来自 mongo 集合的数据。

collection.findOneSession.get 作为选择器,因此当 Session 键没有值时,我会在模板中得到一个空对象,这样我就不会得到任何数据,即空白元素。但我仍然在某些元素上得到了价值。主要是 html 文件中注释的可编辑 divspan 以及一个 input 元素。

我哪里错了? 当我通过设置 Session.set('plate', '') 触发事件时,我试图清理“表单”,即会话密钥的空白值 'plate',空对象,Blaze 模板没有数据,清除元素,对? 谢谢

//client/main.js

Meteor.startup(function(){
  listenToEvents()
})

///// globle listener /////
function listenToEvents(){
  $('.new').on('click', function(e){
    let elementId = $(this).parents('div')[2].id 
    if(elementId == 'vehicle'){
      Session.set('plate','') //<<< used in mongo collection selector
    }
  })
}


///// Blaze helper /////
Template.vehicle.helpers({
  'vehicle' : function(){
    if(Session.get('plate') == undefined){
      console.log('no plate selected')
      return
    }
    let searchForPlate = { "$regex" : Session.get('plate') , "$options" : "i"} // set to '' by event above 
    let vehicle = Vehicles.findOne({'plate': searchForPlate})

    if(!vehicle){ 
      console.log('empty vehicle object')
      return {}
    }
    console.log('vehcile selected: ', vehicle)
    
    if(vehicle.vin.length == 17){ 
      vehicle.vin_a = vehicle.vin.substr(0, 9)
    }
    vehicle.remain = Math.round(diff)
    if(diff <= 0) vehicle.expired = true
 
    return vehicle
  }
})
<template name="vehicle">
  <div id="vehicle" class="subject-container" >

    <div class="section-head">
      <div class="control">
        <button class="toggle">-</button>
        <button class="new">N</button>
      </div>
      <div>
      <!-- the next input does not get cleared -->
        <input id="plate" type="text" size="6" placeholder="plate" value={{vehicle.plate}}>
        
        <!-- the next span does not get cleared -->
        <span data-expired={{vehicle.expired}}>{{vehicle.remain}} {{vehicle.expiry}}</span>
      </div>
    </div>
    
    <div class="body">
      <!-- the next div does not get cleared -->
      <div id="vin" class="editable" contenteditable="true">{{vehicle.vin_a}}<span id="vinb">{{vehicle.vin_b}}</span><span id="vin4">{{vehicle.vin4}}</span></div>
      <input type="text" placeholder="YR, make, modle" value={{vehicle.info}}>
    </div>
  </div>
</template>

嗯,不,一个空的正则表达式匹配所有内容。这很容易测试,如果你打开一个 mongo shell 和 运行 这样的查询,针对任何包含内容的集合:

rs0:PRIMARY> db.users.find({_id: {$regex: ''}}).count()
2723

我会明确地防范这种情况,你可以很容易地这样做:

    if (!Session.get('plate')){
      console.log('no plate selected')
      return
    }