0

我一直堅持這個問題一天半。我想要做的是在我的Meteor.User集合中創建一個userProfile部分。所以,當用戶登陸'設置'頁面時,他們可以更新他們的信息。這是我的失望。使用collection2和autoform擴展Meteor.User集合

順便說一下,我使用User Accounts包進行登錄/註冊過程。

更新:3 表單提交,但沒有數據被插入/更新。我已經將'Schema.User'註釋掉了,因爲如果我將它放入並附加到Meteor.users.attachSchema(Schema.User)。自動錶單加載字段。這就是爲什麼我把Schema.UserProfile改爲了。我檢查了控制檯日誌,它給了我錯誤「[拒絕訪問] 403」它必須是允許/拒絕衝突?我有這裏列出的每個代碼。

設置HTML:

<template name="settings"> 
<div class="text-center light-container" id="settings-section"> 
    {{> quickForm collection="Meteor.users" doc=currentUser id="userProfile" type="update"}} 
</div> 

設置JS在這兩個目錄

Schema = {}; 

Schema.UserProfile = new SimpleSchema({ 
    userProfile: { 
     type: Object 
    }, 
    'userProfile.firstName':{ 
     type: String, 
     label: "First Name", 
     max: 80 
    }, 
    'userProfile.lastName':{ 
     type: String, 
     label: "Last Name", 
     max: 80 
    }, 
    'userProfile.gender':{ 
     type: String, 
     label: "Gender", 
     allowedValues: ["Male", "Female"] 
    }, 
    'userProfile.birthday':{ 
     type: Date, 
     label: "Date Of Birth", 
     autoform: { 
      type: "bootstrap-datepicker" 
     } 
    }, 
    address:{ 
     type: Object 
    }, 
    'address.city':{ 
     type: String, 
     label: "City/Province", 
     max: 80 
    }, 
    'address.state':{ 
     type: String, 
     label: "State", 
     max: 80 
    }, 
    'address.country':{ 
     type: String, 
     label: "Country", 
     max: 80 
    }, 
    /*privacy:{ 
     type: String, 
     label: "Privacy", 
     allowedValues: ["On", "Off"] 
    }, */ 
    aboutYou:{ 
     type: String, 
     label: "About You", 
     autoform: { 
      afFieldInput: { 
       type: "textarea" 
      } 
     }, 
     max: 400 
    }, 
    socialNetworks:{ 
     type: Object, 
     label: "Social Networks" 
    }, 
    'socialNetworks.facebook':{ 
     type: String, 
     label: "Facebook", 
     autoform: { 
      placeholder: 'Username' 
     }, 
     max: 50 
    }, 
    'socialNetworks.instagram':{ 
     type: String, 
     label: "Instagram", 
     autoform: { 
      placeholder: 'Username' 
     }, 
     max: 50 
    }, 
    'socialNetworks.tumblr':{ 
     type: String, 
     label: "Tumblr", 
     autoform: { 
      placeholder: 'Username' 
     }, 
     max: 50 
    }, 
    'socialNetworks.twitter':{ 
     type: String, 
     label: "Twitter", 
     autoform: { 
      placeholder: 'Username' 
     }, 
     max: 50 
    } 
}); 


/* 
    Schema.User = new SimpleSchema({ 
     username: { 
      type: String, 
      regEx: /^[a-z0-9A-Z_]{3,15}$/, 
      optional: true 
     }, 
     emails: { 
      type: [Object], 
      // this must be optional if you also use other login services like facebook, 
      // but if you use only accounts-password, then it can be required 
      optional: true 
     }, 
     "emails.$.address": { 
      type: String, 
      regEx: SimpleSchema.RegEx.Email 
     }, 
     "emails.$.verified": { 
      type: Boolean 
     }, 
     createdAt: { 
      type: Date 
     }, 
     profile: { 
      type: Schema.UserProfile, 
      optional: true 
     }, 
     services: { 
      type: Object, 
      optional: true, 
      blackbox: true 
     }/*, 

     // Add `roles` to your schema if you use the meteor-roles package. 
     // Option 1: Object type 
     // If you specify that type as Object, you must also specify the 
     // `Roles.GLOBAL_GROUP` group whenever you add a user to a role. 
     // Example: 
     // Roles.addUsersToRoles(userId, ["admin"], Roles.GLOBAL_GROUP); 
     // You can't mix and match adding with and without a group since 
     // you will fail validation in some cases. 
     roles: { 
      type: Object, 
      optional: true, 
      blackbox: true 
     } 

     */ 
    }); 

*/ 

Meteor.users.attachSchema(Schema.UserProfile); 

Meteor.users.allow({ 
    insert: function(userId, doc) { 
     // only allow posting if you are logged in 
     console.log("doc: " + doc + " userId: " + userId); 
     return !! userId; 
    }, 
    update: function(userId, doc) { 
     // only allow updating if you are logged in 
     console.log("doc: " + doc + " userId: " + userId); 
     return !! userId; 
    }, 
    remove: function(userID, doc) { 
     //only allow deleting if you are owner 
     return doc.submittedById === Meteor.userId(); 
    } 
}); 
在客戶端目錄

設置JS

/* 
    var postHooks = { 
     before: { 
      insert: function(doc) { 
       if(Meteor.userId()){ 
        doc.userId = Meteor.userId(); 
       } 

       return doc; 
      } 
     }, 
     docToForm: function(doc) { 
      if (_.isArray(doc.tags)) { 
       doc.tags = doc.tags.join(", "); 
      } 
      return doc; 
     }, 
     formToDoc: function(doc) { 
      if (typeof doc.tags === "string") { 
       doc.tags = doc.tags.split(","); 
      } 
      return doc; 
     } 
    }; 

    AutoForm.addHooks('UserProfile', postHooks); 

*/ 

如果任何人都可以將我指向正確的方向,這將是真正有幫助的!

+0

設置JS那麼,怎樣才能實現我呢?我應該如何編碼? –

回答

0

您需要爲您的配置文件創建一個架構,然後將其分配給profile字段中的用戶對象架構。我已經在下面爲你做了。

var Schema = {}; 

Schema.UserProfile = new SimpleSchema({ 
    firstName:{ 
     type: String, 
     label: "First Name", 
     max: 80 
    }, 
    lastName:{ 
     type: String, 
     label: "Last Name", 
     max: 80 
    }, 
    gender:{ 
     type: String, 
     label: "Gender", 
     allowedValues: ["Male", "Female"] 
    }, 
    birthday:{ 
     type: Date, 
     label: "Date Of Birth", 
     autoform: { 
      type: "bootstrap-datepicker" 
     } 
    }, 
    address:{ 
     type: Object 
    }, 
    'address.city':{ 
     type: String, 
     label: "City/Province", 
     max: 80 
    }, 
    'address.state':{ 
     type: String, 
     label: "State", 
     max: 80 
    }, 
    'address.country':{ 
     type: String, 
     label: "Country", 
     max: 80 
    }, 
    /*privacy:{ 
     type: String, 
     label: "Privacy", 
     allowedValues: ["On", "Off"] 
    }, */ 
    aboutYou:{ 
     type: String, 
     label: "About You", 
     autoform: { 
      afFieldInput: { 
       type: "textarea" 
      } 
     }, 
     max: 400 
    }, 
    socialNetworks:{ 
     type: Object, 
     label: "Social Networks" 
    }, 
    'socialNetworks.facebook':{ 
     type: String, 
     label: "Facebook", 
     autoform: { 
      placeholder: 'Username' 
     }, 
     max: 50 
    }, 
    'socialNetworks.instagram':{ 
     type: String, 
     label: "Instagram", 
     autoform: { 
      placeholder: 'Username' 
     }, 
     max: 50 
    }, 
    'socialNetworks.tumblr':{ 
     type: String, 
     label: "Tumblr", 
     autoform: { 
      placeholder: 'Username' 
     }, 
     max: 50 
    }, 
    'socialNetworks.twitter':{ 
     type: String, 
     label: "Twitter", 
     autoform: { 
      placeholder: 'Username' 
     }, 
     max: 50 
    } 
}); 

Schema.User = new SimpleSchema({ 
    username: { 
     type: String, 
     regEx: /^[a-z0-9A-Z_]{3,15}$/, 
     optional: true 
    }, 
    emails: { 
     type: [Object], 
     // this must be optional if you also use other login services like facebook, 
     // but if you use only accounts-password, then it can be required 
     optional: true 
    }, 
    "emails.$.address": { 
     type: String, 
     regEx: SimpleSchema.RegEx.Email 
    }, 
    "emails.$.verified": { 
     type: Boolean 
    }, 
    createdAt: { 
     type: Date 
    }, 
    profile: { 
     type: Schema.UserProfile, 
     optional: true 
    }, 
    services: { 
     type: Object, 
     optional: true, 
     blackbox: true 
    }, 
    // Add `roles` to your schema if you use the meteor-roles package. 
    // Option 1: Object type 
    // If you specify that type as Object, you must also specify the 
    // `Roles.GLOBAL_GROUP` group whenever you add a user to a role. 
    // Example: 
    // Roles.addUsersToRoles(userId, ["admin"], Roles.GLOBAL_GROUP); 
    // You can't mix and match adding with and without a group since 
    // you will fail validation in some cases. 
    roles: { 
     type: Object, 
     optional: true, 
     blackbox: true 
    } 
}); 

Meteor.users.attachSchema(Schema.User); 

爲了您的允許和拒絕的規則,你可能希望可以確保用戶id文檔的_id相匹配的就是想看看他們的登錄代替。

+0

嘿,我已經更新了我的代碼,但它仍然被破壞。我已經編輯了我的原始文章,並對其進行了編碼。 –

+0

我認爲您需要將「this」之外的內容傳遞給模板中的其他內容。 – challett

+0

好吧,所以我在我的客戶端文件夾中加入了我的** settings.js文件**。我提交了表單,並將所有數據傳遞到了url上。我檢查我的收藏,沒有插入。然後,我將我的** settings.js **文件移動到兩個目錄和相同的問題。 問題是,提交後頁面刷新和所有的數據傳遞到URL。 –

0

試試這個(搜索「NOTE 「用於描述有關更改的註釋):

設置HTML:

<template name="settings"> 
<div class="text-center light-container" id="settings-section"> 
    {{! NOTE: the fields attribute tells quickForm which fields to display }} 
    {{> quickForm collection="Meteor.users" fields="userProfile" doc=currentUser id="userProfile" type="update"}} 
</div> 

設置JS在這兩個目錄

Schema = {}; 

Schema.UserProfile = new SimpleSchema({ 
    userProfile: { 
     type: Object 
    }, 
    'userProfile.firstName':{ 
     type: String, 
     label: "First Name", 
     max: 80 
    }, 
    'userProfile.lastName':{ 
     type: String, 
     label: "Last Name", 
     max: 80 
    }, 
    'userProfile.gender':{ 
     type: String, 
     label: "Gender", 
     allowedValues: ["Male", "Female"] 
    }, 
    'userProfile.birthday':{ 
     type: Date, 
     label: "Date Of Birth", 
     autoform: { 
      type: "bootstrap-datepicker" 
     } 
    }, 
    address:{ 
     type: Object 
    }, 
    'address.city':{ 
     type: String, 
     label: "City/Province", 
     max: 80 
    }, 
    'address.state':{ 
     type: String, 
     label: "State", 
     max: 80 
    }, 
    'address.country':{ 
     type: String, 
     label: "Country", 
     max: 80 
    }, 
    /*privacy:{ 
     type: String, 
     label: "Privacy", 
     allowedValues: ["On", "Off"] 
    }, */ 
    aboutYou:{ 
     type: String, 
     label: "About You", 
     autoform: { 
      afFieldInput: { 
       type: "textarea" 
      } 
     }, 
     max: 400 
    }, 
    socialNetworks:{ 
     type: Object, 
     label: "Social Networks" 
    }, 
    'socialNetworks.facebook':{ 
     type: String, 
     label: "Facebook", 
     autoform: { 
      placeholder: 'Username' 
     }, 
     max: 50 
    }, 
    'socialNetworks.instagram':{ 
     type: String, 
     label: "Instagram", 
     autoform: { 
      placeholder: 'Username' 
     }, 
     max: 50 
    }, 
    'socialNetworks.tumblr':{ 
     type: String, 
     label: "Tumblr", 
     autoform: { 
      placeholder: 'Username' 
     }, 
     max: 50 
    }, 
    'socialNetworks.twitter':{ 
     type: String, 
     label: "Twitter", 
     autoform: { 
      placeholder: 'Username' 
     }, 
     max: 50 
    } 
}); 

// NOTE: You need to use this schema because it is the schema for the collection you are updating. 
Schema.User = new SimpleSchema({ 
    username: { 
     type: String, 
     regEx: /^[a-z0-9A-Z_]{3,15}$/, 
     optional: true 
    }, 
    emails: { 
     type: [Object], 
     // this must be optional if you also use other login services like facebook, 
     // but if you use only accounts-password, then it can be required 
     optional: true 
    }, 
    "emails.$.address": { 
     type: String, 
     regEx: SimpleSchema.RegEx.Email 
    }, 
    "emails.$.verified": { 
     type: Boolean 
    }, 
    createdAt: { 
     type: Date 
    }, 
    profile: { 
     type: Schema.UserProfile, 
     optional: true 
    }, 
    services: { 
     type: Object, 
     optional: true, 
     blackbox: true 
    }/*, 

    // Add `roles` to your schema if you use the meteor-roles package. 
    // Option 1: Object type 
    // If you specify that type as Object, you must also specify the 
    // `Roles.GLOBAL_GROUP` group whenever you add a user to a role. 
    // Example: 
    // Roles.addUsersToRoles(userId, ["admin"], Roles.GLOBAL_GROUP); 
    // You can't mix and match adding with and without a group since 
    // you will fail validation in some cases. 
    roles: { 
     type: Object, 
     optional: true, 
     blackbox: true 
    } 

    */ 
}); 

// NOTE: Meteor.users is a collection of objects matching the Schema.user schema, so you need to use this schema. 
Meteor.users.attachSchema(Schema.User); 

Meteor.users.allow({ 
    /* NOTE: The client should not be allowed to add users directly! 
    insert: function(userId, doc) { 
     // only allow posting if you are logged in 
     console.log("doc: " + doc + " userId: " + userId); 
     return !! userId; 
    }, 
    */ 
    update: function(userId, doc, fieldNames) { 
     // only allow updating if you are logged in 
     console.log("doc: " + doc + " userId: " + userId); 
     // NOTE: a user can only update his own user doc and only the 'userProfile' field 
     return !! userId && userId === doc._id && _.isEmpty(_.difference(fieldNames, ['userProfile'])); 
    }, 
    /* NOTE: The client should not generally be able to remove users 
    remove: function(userID, doc) { 
     //only allow deleting if you are owner 
     return doc.submittedById === Meteor.userId(); 
    } 
    */ 
}); 

在客戶端目錄

/* NOTE: I don't think you need any of these hooks. 
    var postHooks = { 
     before: { 
      insert: function(doc) { 
       if(Meteor.userId()){ 
        doc.userId = Meteor.userId(); 
       } 

       return doc; 
      } 
     }, 
     docToForm: function(doc) { 
      if (_.isArray(doc.tags)) { 
       doc.tags = doc.tags.join(", "); 
      } 
      return doc; 
     }, 
     formToDoc: function(doc) { 
      if (typeof doc.tags === "string") { 
       doc.tags = doc.tags.split(","); 
      } 
      return doc; 
     } 
    }; 

    AutoForm.addHooks('UserProfile', postHooks); 

*/