我想制作一个小型邀请系统(用户向 friend 发送一封带有邀请码的电子邮件 --> friend 点击每个人都可以去的公共(public)网站 --> 将他的邀请码放在文本字段中,然后 meteor 搜索此代码如果能找到代码,一切都很好,他可以继续,但是当 meteor 找不到代码时,他是一个随机的互联网用户,他不应该继续
所以我需要一些东西来比较输入值和集合中的数据
这是我的 js 文件,也许里面已经有一些好东西了 ;)
Template.Invite.onCreated(function() {
this.subscribe('invites');
});
Template.Invite.events({
'submit .Invite'(event) {
event.preventDefault();
var Invite = event.target.Invite.value;
}
});
Template.Invite.helpers({
results: function(){
return Invites.find({
code: Session.get('Invite'),
if (Invite = Invite)
{
FlowRouter.go('/');
}
});
}
});
我在 main.js 中的发布部分
Meteor.publish("invites", function() {
return Invites.find();
})
和不重要的html
<template name="Invite">
<form class="Invite" >
<input type="text" name="Invite" placeholder="Invite Code" />
<input type="submit" value="Bestätigen" />
</form>
</template>
Invite Collection 中的插入已经工作但不是查找和比较
感谢您的宝贵时间和帮助;)
最佳答案
我已经创建了几次邀请系统,这就是我的做法。
当用户发送邀请时,您在 Invitation 集合中创建一个新文档,如下所示:
import { Random } from 'meteor/random';
const code = Random.hexString(16);
invitation = {
'code': code,
'sentTo': 'user@website.com',
'accepted': false,
}
然后当用户尝试注册时,您需要调用一个方法来获取他们的邀请代码并将其与数据库中的代码进行比较
Meteor.methods({
'acceptInvitation'(code) {
check(code, String);
// find invitation in database
let invitation = Invitations.findOne({ 'code': code});
//check if invitation exists and if it hasn't already been accepted
if(invitation && invitation.accepted == false) {
//update invitation to now be accepted
Invitations.update(
{ '_id': invitation._id},
{ $set: { accepted: true }
);
return true;
} else {
throw new Meteor.Error('invalid', 'Your invitation code is not valid');
}
}
});
为了使您的邀请系统更加完善,当您发送邀请电子邮件时,您可以将邀请代码作为参数传递到 URL 中。然后,当用户单击邀请链接时,您可以从 URL 中获取代码并自动将其放入注册表中。这可以防止他们在复制/粘贴时出错!
关于javascript - Meteor 如何创建邀请系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42165502/