【问题标题】:Meteor update user profile流星更新用户资料
【发布时间】:2015-10-21 22:56:05
【问题描述】:

我不知道我的应用出了什么问题。我正在尝试更新用户个人资料。如果用户已有配置文件,则应显示配置文件的当前值。我有一个 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"

如何去更新个人资料,盯着看......

【问题讨论】:

  • 你能发布你架构的相关部分吗?
  • 当然。我编辑了原始问题以包含它。
  • 你在使用不安全的包吗?如果没有,请确保您设置了收集权限,以便授权用户可以更新用户配置文件。

标签: javascript meteor


【解决方案1】:

您需要更改您的before 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 回调只有一个参数,即插入/更新的文档。这就是您总是记录“错误”的原因,它只是您要插入的文档。您还需要返回它,或将其传递给 this.result 以实际插入/更新数据库中的对象。

From the docs:

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)
    }
  },

【讨论】:

  • 我确实应该看到这一点。感谢您指出!但是,即使更改了您的建议,它也不起作用。我可以将新用户插入 Mongo,但似乎无法更新现有用户。在 Autohook 中,我有一个输出文档的 console.log。我没有看到错误,但不知何故,Mongo 数据库似乎没有更新。有什么想法吗?
  • 并非如此。您可以做的最好的事情是创建一个minimal reproducible example,因为这个问题远非如此。虽然这样做会产生一个让更多人能够帮助解决的问题,但它的价值也在这个过程中,因为通过剥离它,您可能会自己找到解决方案。创建一个不使用 autoform 钩子、不需要 flow:router、alanning:roles 等的示例。为了找到上述问题,有很多事情要做,而我嘲笑的东西可能是屏蔽你遇到的另一个问题。无论如何,我会尝试获取我在meteorpad 中的工作并发布链接。
  • 我从 flow:router not like Meteor.userId() 在路由订阅块中看到另一个错误,因为路由器没有反应。但是我相信这是相当无害的,因为它在执行时仍会使用当前值 - 但如果值发生变化则不会响应。
  • 这是meteorpad
  • 我试过你在meteorpad上放的东西,它似乎工作不是吗?
【解决方案2】:

有几个小问题,所以我不知道如何解决您的问题,但这里有一些事情要解决。

发布方法

  • 从不使用局部变量user。您是否尝试使用 它?
  • 不需要包含userId 作为函数参数,因为你有 访问this.userId
  • 默认情况下会发布当前用户的个人资料,因此您不需要使用发布/订阅,除非您想包含/排除字段,但是我会定义一个Meteor.publish(null, ...),以便它覆盖默认的当前用户发布

注意:如果你删除了发布usersAllforCustomer函数,别忘了从路由updateCustomerProfile中删除它

使用全局助手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>

希望这会有所帮助。

【讨论】:

    【解决方案3】:

    meteorpad 确实解决了这个问题。助手有错误。其实原来的代码是:

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

    所以在上面的 sn-p 中,我使用的是“事件”而不是“助手”。下面是正确的代码:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-05
      • 1970-01-01
      • 1970-01-01
      • 2014-04-14
      • 1970-01-01
      • 2019-10-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多