簡體   English   中英

Meteor更新用戶配置文件

[英]Meteor update user profile

我不知道我的應用程序出了什么問題。 我正在嘗試更新用戶個人資料。 如果用戶已有配置文件,則應顯示配置文件的當前值。 我有一個附加到用戶集合的SimpleSchema。

<template name="updateCustomerProfile">
  <div class="container">
    <h1>Edit User</h1>
    {{#if isReady 'updateCustomerProfile'}}
      {{#autoForm collection="Users" doc=getUsers id="profileForm" type="update"}}
        <fieldset>
          {{> afQuickField name='username'}}
          {{> afObjectField name='profile'}}
        </fieldset>
        <button type="submit" class="btn btn-primary">Update User</button>
        <a class="btn btn-link" role="button" href="{{pathFor 'adminDocuments'}}">Back</a>
      {{/autoForm}}
    {{else}}
      Nothing
    {{/if}}
   </div>
</template>

我有一個模板助手:

Template.updateCustomerProfile.events({
getUsers: function () {
    //return Users.findOne();
    return Meteor.user();
  }
});

我有一個Autoform鈎子

AutoForm.addHooks(['profileForm'], { 
    before: {
      insert: function(error, result) {
        if (error) {
          console.log("Insert Error:", error);
          AutoForm.debug();
        } else {
          console.log("Insert Result:", result);
          AutoForm.debug();
        }
      },
      update: function(error) {
        if (error) {
          console.log("Update Error:", error);
          AutoForm.debug();
        } else {
          console.log("Updated!");
          console.log('AutoForm.debug()');
        }
      }
    }
  });

有以下路線:

customerRoutes.route('/profile/edit', {
  name: "updateCustomerProfile",
  subscriptions: function (params, queryParams) {
    this.register('updateCustomerProfile', Meteor.subscribe('usersAllforCustomer',  Meteor.userId()));
  },
  action: function(params, queryParams) {
    BlazeLayout.render('layout_frontend', {
      top: 'menu',
      main: 'updateCustomerProfile',
      footer: 'footer'
    });
  }
});

最后是以下出版物:

Meteor.publish('usersAllforCustomer', function (userId) {
    check(userId, String);
    var user = Users.findOne({_id: userId});
    if (Roles.userIsInRole(this.userId, 'customer')) {
        return Users.find({_id: userId});
    }
});

這是集合:

Users = Meteor.users;

Schema = {};

Schema.UserProfile = new SimpleSchema({
    firstName: {
        type: String,
        optional: true
    },
    lastName: {
        type: String,
        optional: true
    },
    gender: {
        type: String,
        allowedValues: ['Male', 'Female'],
        optional: true
    },
    organization : {
        type: String,
        optional: true
    }
});

Schema.User = new SimpleSchema({
    username: {
        type: String,
        optional: true
    },
    emails: {
        type: Array,
        optional: true
    },
    "emails.$": {
        type: Object
    },
    "emails.$.address": {
        type: String,
        regEx: SimpleSchema.RegEx.Email
    },
    "emails.$.verified": {
        type: Boolean
    },
    createdAt: {
        type: Date,
        optional: true,
        denyUpdate: true,
        autoValue: function() {
            if (this.isInsert) {
                return new Date();
            }
        }
    },
    profile: {
        type: Schema.UserProfile,
        optional: true
    },
    services: {
        type: Object,
        optional: true,
        blackbox: true
    },
    roles: {
        type: [String],
        optional: true
    }
});

Meteor.users.attachSchema(Schema.User);

我確定用戶對象在發布中傳遞。 我無法更新配置文件:收到以下錯誤(來自Autoform調試):

Update Error: Object {$set: Object}
   $set: Object
        profile.firstName: "test_firstname"
        profile.gender: "Female"
        profile.lastName: "test_lastname"
        profile.organization: "test_organisation
        "username: "test_username"

如何更新個人資料,盯着盲人....

您需要在AutoForm Hooks之前更改您的。

AutoForm.addHooks(['profileForm'], {
  before: {
    insert: function(doc) {
      console.log('doc: ', doc);
      return doc;
    },

    update: function(doc) {
      console.log('doc: ', doc);
      return doc;
    },
  },
});

雖然after回調具有js標准(error, result)函數簽名,但before回調只有一個參數,即插入/更新的doc。 這就是您始終記錄“錯誤”的原因,它只是您要插入的文檔。 您還需要返回它,或將其傳遞給this.result以實際插入/更新數據庫中的對象。

來自文檔:

var hooksObject = {
  before: {
    // Replace `formType` with the form `type` attribute to which this hook applies
    formType: function(doc) {
      // Potentially alter the doc
      doc.foo = 'bar';

      // Then return it or pass it to this.result()
      return doc; (synchronous)
      //return false; (synchronous, cancel)
      //this.result(doc); (asynchronous)
      //this.result(false); (asynchronous, cancel)
    }
  },

有幾個小問題,所以我不知道如何解決你的問題,但這里有一些事情需要解決。

發布方法

  • 從不使用本地變量user 你想嘗試使用它嗎?
  • 無需將userId包含為函數參數,因為您可以訪問this.userId
  • 默認情況下發布當前用戶的配置文件,因此您不需要使用發布/訂閱,除非您想要包含/排除字段,但我會定義Meteor.publish(null, ...)以便它覆蓋默認值當前用戶出版物

注意:如果刪除發布usersAllforCustomer函數,請不要忘記將其從路由updateCustomerProfile刪除

使用Global Helper currentUser

以下是如何更新模板以使用currentUser而不是getUsers

<template name="updateCustomerProfile">
  <div class="container">
    <h1>Edit User</h1>
    {{#with currentUser}}
      {{#autoForm collection="Users" doc=this id="profileForm" type="update"}}
        <fieldset>
          {{> afQuickField name='username'}}
          {{> afObjectField name='profile'}}
        </fieldset>
        <button type="submit" class="btn btn-primary">Update User</button>
        <a class="btn btn-link" role="button" href="{{pathFor 'adminDocuments'}}">Back</a>
      {{/autoForm}}
    {{else}}
      Nothing
    {{/with}}
   </div>
</template>

希望這可以幫助。

這個meteorpad確實解決了這個問題。 幫助者有一個錯誤。 事實上,原始代碼是:

Template.updateCustomerProfile.events({
getUsers: function () {
    return Meteor.user();
  }
});

所以在上面的代碼片段中,我使用的是“事件”而不是“助手”。 以下是正確的代碼:

Template.updateCustomerProfile.helpers({
  getUsers: function(){
    return Meteor.user();
  }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM