防止MeteorJS集合中的重复(通过表单)

Prevent Duplicity (through form) in MeteorJS collection

本文关键字:表单 MeteorJS 集合 防止      更新时间:2023-09-26

我正在通过表单收集数据&使用输入类将其保存到流星集合(工作正常)。现在我想防止重复输入"电子邮件"&"类别"组合(用户不能两次从同一封电子邮件申请类别)。请帮忙
还告诉我如何生成在表格中填写姓名的感谢通知

         /*Meteor JS File*/
Info = new Meteor.Collection("info");
if (Meteor.isClient) {
Template.add_info.events({
   'submit form':function(){
   Info.insert({
    category:$('.the_item11').val(),
    name:$('.the_item1').val(),
    mobile:$('.the_item2').val(),
    Mail:$('.the_item3').val(),
    createdAt: new Date(),
        });
    if ($.trim($("#uname").val()) === "" || $.trim($("#umobile").val()) === ""){
        alert('you did not fill out one of the fields');
        return false;                                  }            
    alert("Thank You For Registering")      
                }
           });          
         }

                           /*HTML form*/
<head>
  <title>yahaviform</title>
</head>
<body>
<img src="EDMcrowd.jpg" name="CA_5" id="CA_5"/>
  <h1 id="reg">Register here</h1>
 {{> add_info}} 
</body>

<template name="add_info">
<div id="d1">
<form> 
<label>You are</label><br>
  <select class="the_item11" id="ucat" required>
    <option></option>
    <option>DJ</option>
    <option>Singer</option>
    <option>Instrumentalist</option>
    <option>Band</option>   
  </select> <br>
  <label>Name</label><br>
  <input type="text" class="the_item1" id="uname" required><br>

  <label>Mobileno.</label><br>
  <input type="tel" class="the_item2" id="umobile" required><br>
    <label>EmailID</label><br>
  <input  class="the_item3" type="email" id="umail" required><br>
<br>    
    <input type="submit" value="Register">
</form>
</div>
</template>

您可以使用复合唯一索引:

db.my_collection.ensureIndex(
    {
        email: 1,
        category: 1
    },
    {
        unique: 1
    }
)

看看很棒的包meteor-autoform

https://github.com/aldeed/meteor-autoform