0

作为我的应用程序开发环境的一部分,我创建了一些种子数据,这些数据可以创建用户并将一些数据添加到他们的个人资料字段中。

我现在可以在我的应用程序的配置文件视图中显示该数据。

我的问题是允许用户在注册时添加这些数据的最佳方式是什么?

我已经尝试使用表单作为“配置文件”菜单集的一部分来执行此操作,但我没有成功更新该字段的数据。未添加新的/更新的数据。

我的模板看起来像这样

<template name="profileForm">
<form class="profileform form-horizontal" role="form">
    <div class="form-group">
        <!-- <label for="inputFirstName">First Name</label> -->
        <div class="col-sm-4">
            <input id="firstname" type="text" name="firstname" placeholder={{firstName}} />
        </div>
    </div>
    <div class="form-group">
        <!-- <label for="inputFirstName">Last Name</label> -->
        <div class="col-sm-4">
            <input id="lastname" type="text" name="lastname" placeholder={{lastName}} />
        </div>
    </div>
    <div class="form-group">
        <!-- <label for="inputFirstName">E-Mail Address</label> -->
        <div class="col-sm-4">
            <input id="email" type="email" name="email" placeholder={{email}} />
        </div>
    </div>
    <div class="form-group">
        <!-- <label for="inputFirstName">Phone Number</label> -->
        <div class="col-sm-4">
            <input id="phone" type="tel" name="phone" placeholder={{phoneNumber}} />
        </div>
    </div>
    <div class="form-group">
        <!-- <label for="inputFirstName">School</label> -->
        <div class="col-sm-4">
            <input id="school" type="text" name="school" placeholder={{schoolName}} />
        </div>
    </div>
    <div class="form-group">
        <!-- <label for="inputFirstName">First year</label> -->
        <div class="col-sm-4">
            <input id="firstschoolyear" type="text" name="firstschoolyear" placeholder={{firstSchoolYear}} />
        </div>
    </div>
    <div class="form-group">
        <!-- <label for="inputFirstName">Last Year</label> -->
        <div class="col-sm-4">
            <input id="lastschoolyear" type="text" name="lastschoolyear" placeholder={{lastSchoolYear}} />
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-4">
            <label>Did you Matriculate Here?</label>
        </div>
        <div class="col-xs-3">
            <select class="form-control">
                <option>Yes</option>
                <option>No</option>
            </select>
        </div>
    </div>
    <div class="form-group">
        <!-- <label for="inputFirstName">House Name</label> -->
        <div class="col-sm-4">
            <input id="housename" type="text" name="housename" placeholder={{houseName}} />
        </div>
    </div>
    <div class="form-group">
        <!-- <label for="inputFirstName">Country Living In</label> -->
        <div class="col-sm-4">
            <input id="country" type="text" name="country" placeholder={{country}} />
        </div>
    </div>
    <div class="form-group">
        <!-- <label for="inputFirstName">City</label> -->
        <div class="col-sm-4">
            <input id="cityofresidence" type="text" name="cityofresidence" placeholder={{cityOfResidence}} />
        </div>
    </div>
    <div class="form-group">
        <!-- <label for="inputFirstName">Employment Industry</label> -->
        <div class="col-sm-4">
            <input tid="emplindustry" ype="text" name="emplindustry" placeholder={{emplIndustry}} />
        </div>
    </div>
    <button class="profilesubmit">Update Profile</button>
</form>

我的 JS 文件看起来像这样

Template.profileForm.events({
  "submit .profileform": function (event) {
// This function is called when the profile form is submitted

var firstname = event.target.firstname.value,
 lastname = event.target.astname.value,
 email = event.target.email.value,
 phone = event.target.phone.value,
 school = event.target.school.value,
 firstschoolyear = event.target.firstschoolyear.value,
 lastschoolyear = event.target.lastschoolyear.value,
 matriculated = event.target.matriculated.value,
 housename = event.target.houseelname.value,
 country = event.target.country.value,
 cityofresidence = event.target.cityofresidence.value,
 emplindustry = event.target.emplindustry.value;

if(Meteor.userId())
 {
    Meteor.users.update({_id: Meteor.userId()},{$set:{
        "profile.firstname": firstname,      
        "profile.lastname": lastname,
        "profile.phone": phone,
        "profile.school": school,
        "profile.firstschoolyear": firstschoolyear,
        "profile.lastschoolyear": lastschoolyear,
        "profile.matriculated": matriculated,
        "profile.housename": housename,
        "profile.country": country,
        "profile.cityofresidence": cityofresidence,
        "profile.emplindustry": emplindustry,
        "profile.joined": new Date(), // current time
      }});
  }
// Clear form
//event.target.text.value = "";

// Prevent default form submit
return false;
  }
});

Template.profileForm.helpers({
  email: function() {return Meteor.user().emails[0].address},
  userName: function () {return Meteor.user().username},
  firstName: function () {return Meteor.user().profile.firstname},
  lastName: function () {return Meteor.user().profile.lastname},
  phoneNumber: function () {return Meteor.user().profile.phone},
  schoolName: function () {return Meteor.user().profile.schoolName},
  firstSchoolYear: function () {return Meteor.user().profile.firstschoolyear},
  lastSchoolYear: function () {return Meteor.user().profile.lastschoolyear},
  matriculated: function () {return Meteor.user().profile.matriculated},
  houseName: function () {return Meteor.user().profile.housename},
  country: function () {return Meteor.user().profile.country},
  cityOfResidence: function () {return Meteor.user().profile.cityofresidence},
  emplIndustry: function () {return Meteor.user().profile.emplindustry},
  signedUp: function () {return Meteor.user().profile.joined},
});

我使用了 Allow 功能来允许用户编辑他们的 profiel 字段。

Meteor.publish("userData", function () {
  return Meteor.users.find({_id: this.userId},{ fields: {'profile': 1}});
});

Meteor.users.allow({
  update: function (userId, user, fields, modifier) {
    // can only change your own documents
    if(user._id === userId)
    {
      Meteor.users.update({_id: userId}, modifier);
      return true;
    }
    else return false;
  }
});

我的订阅看起来像这样。

Meteor.subscribe('userData');

我坚信我缺乏编程技能导致我遗漏了一些重要的东西。

4

2 回答 2

5

我建议看看这三个包:aldeed:simple-schemaaldeed:autoformaldeed:collection2。他们的示例已经包含您问题的答案!

简单模式将允许您快速为您的文档(例如用户)创建模式,指示每个字段的类型、数组或字符串的最大长度等。Autoform 允许您从该模式快速生成表单,而 Collection2 允许您将模式绑定到集合,从而自动进行检查和清理。最后,如果您必须更改您的文档架构(例如通过添加一个新字段),您只需要更改您的架构,一切都会相应地改变(>表单的行为方式取决于您的操作方式,管理您的收藏的方式)。

在你的情况下,

//Shared code
var userProfileSchema = new SimpleSchema({
  firstName : {
    type : String,
    max : 100,
    defaultValue : ''
  },
  lastName : {
    type : String,
    max : 100,
    defaultValue : ''
  },
  email : {
    type : String,
    regEx : SimpleSchema.RegEx.Email,
    optional : true
  },
  ...
});

var userSchema = {
  //Add all the fields of a normal Meteor user
  //Then add your own schema:
  profile : userProfileSchema
};

Meteor.users.attachSchema(userSchema);

现在您的架构已正确定义并附加到集合上。
Triforce 的第三部分现在是aldeed:autoform:简单地生成表单!

{{> quickForm 
  schema=userProfileSchema doc=getProfile 
  type=method meteormethod="updateSelfProfile"
}}
于 2014-12-01T10:10:39.853 回答
-1

您所做的方式的一个问题是,如果您引入诸如“角色”之类的东西,将数据存储在配置文件中,如果您不清理他们尝试和设置的内容,您的代码可能会让某人将自己提升为管理员。

于 2014-12-02T10:41:05.547 回答