jjzjj

javascript - Meteor 更新用户资料

coder 2024-07-25 原文

我不知道我的应用程序出了什么问题。我正在尝试更新用户个人资料。如果用户已经有一个配置文件,它应该显示配置文件的当前值。我有一个附加到用户集合的 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 钩子(Hook)

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"

如何着手更新个人资料,盲目地盯着......

最佳答案

您需要更改您的 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 回调只有一个参数,doc插入/更新。这就是为什么您总是记录“错误”,它只是您要插入的文档。您还需要返回它,或者将它传递给 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)
    }
  },

关于javascript - Meteor 更新用户资料,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33026516/

有关javascript - Meteor 更新用户资料的更多相关文章

  1. ruby-on-rails - 如何验证 update_all 是否实际在 Rails 中更新 - 2

    给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru

  2. ruby-on-rails - 使用 rails 4 设计而不更新用户 - 2

    我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它​​不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数

  3. ruby-on-rails - 简单的 Ruby on Rails 问题——如何将评论附加到用户和文章? - 2

    我意识到这可能是一个非常基本的问题,但我现在已经花了几天时间回过头来解决这个问题,但出于某种原因,Google就是没有帮助我。(我认为部分问题在于我是一个初学者,我不知道该问什么......)我也看过O'Reilly的RubyCookbook和RailsAPI,但我仍然停留在这个问题上.我找到了一些关于多态关系的信息,但它似乎不是我需要的(尽管如果我错了请告诉我)。我正在尝试调整MichaelHartl'stutorial创建一个包含用户、文章和评论的博客应用程序(不使用脚手架)。我希望评论既属于用户又属于文章。我的主要问题是:我不知道如何将当前文章的ID放入评论Controller。

  4. ruby - RVM "ERROR: Unable to checkout branch ."单用户 - 2

    我在新的Debian6VirtualBoxVM上安装RVM时遇到问题。我已经安装了所有需要的包并使用下载了安装脚本(curl-shttps://rvm.beginrescueend.com/install/rvm)>rvm,但以单个用户身份运行时bashrvm我收到以下错误消息:ERROR:Unabletocheckoutbranch.安装在这里停止,并且(据我所知)没有安装RVM的任何文件。如果我以root身份运行脚本(对于多用户安装),我会收到另一条消息:Successfullycheckedoutbranch''安装程序继续并指示成功,但未添加.rvm目录,甚至在修改我的.bas

  5. objective-c - 在设置 Cocoa Pods 和安装 Ruby 更新时出错 - 2

    我正在尝试为我的iOS应用程序设置cocoapods但是当我执行命令时:sudogemupdate--system我收到错误消息:当前已安装最新版本。中止。当我进入cocoapods的下一步时:sudogeminstallcocoapods我在MacOS10.8.5上遇到错误:ERROR:Errorinstallingcocoapods:cocoapods-trunkrequiresRubyversion>=2.0.0.我在MacOS10.9.4上尝试了同样的操作,但出现错误:ERROR:Couldnotfindavalidgem'cocoapods'(>=0),hereiswhy:U

  6. ruby-on-rails - Rails Associations 的更新方法是什么? - 2

    这太简单了,太荒谬了,我在任何地方都找不到关于它的任何信息,包括API文档和Rails源代码:我有一个:belongs_to关联,我开始理解当您没有关联时您在Controller中调用的正常模型方法与您有关联时调用的方法略有不同。例如,我的关联在创建Controller操作时运行良好:@user=current_user@building=Building.new(params[:building])respond_todo|format|if@user.buildings.create(params[:building])#etcetera但我找不到关于更新如何工作的文档:@user

  7. ruby - 在没有基准或时间的情况下用 Ruby 测量用户时间或系统时间 - 2

    因为我现在正在做一些时间测量,我想知道是否可以在不使用Benchmark类或命令行实用程序time的情况下测量用户时间或系统时间。使用Time类只显示挂钟时间,而不显示系统和用户时间,但是我正在寻找具有相同灵active的解决方案,例如time=TimeUtility.now#somecodeuser,system,real=TimeUtility.now-time原因是我有点不喜欢Benchmark,因为它不能只返回数字(编辑:我错了-它可以。请参阅下面的答案。)。当然,我可以解析输出,但感觉不对。*NIX系统的time实用程序也应该可以解决我的问题,但我想知道是否已经在Ruby中实

  8. ruby-on-rails - 使用 javascript 更改数据方法不会更改 ajax 调用用户的什么方法? - 2

    我遇到了一个非常奇怪的问题,我很难解决。在我看来,我有一个与data-remote="true"和data-method="delete"的链接。当我单击该链接时,我可以看到对我的Rails服务器的DELETE请求。返回的JS代码会更改此链接的属性,其中包括href和data-method。再次单击此链接后,我的服务器收到了对新href的请求,但使用的是旧的data-method,即使我已将其从DELETE到POST(它仍然发送一个DELETE请求)。但是,如果我刷新页面,HTML与"new"HTML相同(随返回的JS发生变化),但它实际上发送了正确的请求类型。这就是这个问题令我困惑的

  9. ruby - HTTP 请求中的用户代理,Ruby - 2

    我是Ruby的新手。我试过查看在线文档,但没有找到任何有效的方法。我想在以下HTTP请求botget_response()和get()中包含一个用户代理。有人可以指出我正确的方向吗?#PreliminarycheckthatProggitisupcheck=Net::HTTP.get_response(URI.parse(proggit_url))ifcheck.code!="200"puts"ErrorcontactingProggit"returnend#Attempttogetthejsonresponse=Net::HTTP.get(URI.parse(proggit_url)

  10. ruby-on-rails - capybara poltergeist - 覆盖用户代理 - 2

    有人知道如何将capybarapoltergeist的用户代理覆盖到移动用户代理以进行测试吗?我发现了一些有关为seleniumwebdriver配置它的信息:http://blog.plataformatec.com.br/2011/03/configuring-user-agents-with-capybara-selenium-webdriver/这在capybara闹鬼中怎么可能? 最佳答案 请参阅poltergeistgithub页面上的链接:https://github.com/teampoltergeist/polte

随机推荐