繁体   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