【问题标题】:Meteor: Profile update fails mysteriously流星:个人资料更新神秘失败
【发布时间】:2015-03-02 16:06:23
【问题描述】:

我正在构建一个 Meteor 应用程序,并希望用户能够更新/设置他们的名称。我已经设置了代码,一切似乎都在工作,只是它在更新时神秘地失败了。这是我的代码:

模板

<template name="_editName">
  {{#ionModal focusFirstInput=true customTemplate=true}}
    <form id="updateName">
      <div class="bar bar-header bar-stable">
        <button data-dismiss="modal" type="button" class="button button-clear button-assertive">Cancel</button>
        <h2 class="title">Update your name</h2>
        <button type="submit" class="button button-positive button-clear">Save</button>
      </div>
      <div class="content has-header overflow-scroll list">
        <label class="item item-input">
          <input type="text" id="firstName" placeholder="Given Name">
        </label>
        <label class="item item-input">
          <input type="text" id="lastName" placeholder="Surname">
        </label>
      </div>
    </form>
  {{/ionModal}}
</template>

客户端代码

Template._editName.events({
  'submit #updateName': function(event, template) {
    event.preventDefault();
    console.log('Submitted');
    var firstName = document.getElementById('firstName').value;
    var lastName = document.getElementById('lastName').value;

    Meteor.call('editName', firstName, lastName, function(error) {
      if (error) {      
        throw new Error(error.reason);
      } else {
        Router.go('profile');
      }
    });
    IonModal.close();
    IonKeyboard.close();
  }
});

服务器端代码

Meteor.methods({
    'editName': function(firstName, lastName) {
        console.log(firstName); // returns expected value
        console.log(lastName); // returns expected value
        Meteor.users.update({_id: this.userId}, {$set: {
            'profile.firstName': firstName,
            'profile.lastName': lastName
        }});
        Meteor.users.findOne({_id: this.userId}, {fields: {
        'profile.firstName': 1, _id: 0}}); // returns undefined
    }
});

一切正常:提交时,函数获取字段值,调用服务器上的方法,成功传递字段值。更新 似乎 正在工作,因为没有引发错误。但是,“profile.firstName”返回未定义并且没有显示在我的模板中。

我正在使用accounts-base 包和aldeed:collection2。我设置了以下架构:

Schema.UserProfile = new SimpleSchema({
    firstName: {
        type: String,
        regEx: /^[a-zA-Z-]{2,25}$/,
        optional: true
    },
    lastName: {
        type: String,
        regEx: /^[a-zA-Z]{2,25}$/,
        optional: true
    }
});

我完全不知道出了什么问题,我会感谢每一个帮助!

【问题讨论】:

  • Meteor.users.findOne({_id: this.userId}... 仅当执行此调用的用户已正确登录时才有效,否则 this.userID 将当然是空的。接下来是你通过meteor mongo检查配置文件是否设置正确?
  • 另外,您不能在字段选项中混合包含 (1) 和排除 (0)。
  • @Meteorpoly 感谢您的快速回复!我正在使用accounts-base 包并且只是仔细检查:用户已正确登录,console.log(this.userId) 返回正确的用户ID。您的意思是在终端中运行meteor mongo 吗?或者我该如何检查?
  • 好的,但是 mongodb 说什么,更新到数据库了吗?
  • @saimeunt 感谢您的提示,但您实际上可以: 例外情况是您可以在包含说明符中指定 _id: 0,这也会将 _id 排除在结果对象之外。 (Meteor docs).

标签: collections meteor updates


【解决方案1】:

我检查了您的代码,发现多个问题需要您注意。

请从 here 获取已修复的项目,然后按照说明获取并运行它。

主要问题是:

  1. 在集合的架构定义中,您首先指定了 UserSchema 并引用了 UserProfile 架构。由于 UserProfile 在您引用它时不存在,因此代码会默默地失败。也许您可以与包所有者一起解决这个问题,以便更好地处理错误和参考检查。
  2. 您绝对应该阅读流星的数据绑定,因为在 profile.html 模板下您犯了很多错误
  3. 在创建用户时初始化您的配置文件架构:
Accounts.onCreateUser(function(options, user) {
  // We're enforcing at least a profile skeleton object to avoid needing to check
  // for its existence later.
  // this file should reside on Server (e.g.  [root]/server/accounts.js)
  profile = {
    firstName: '',
    lastName: '',
    birthday: '',
    sex: '',
    grade: '',
    country: '',
    hostel: '',
    house:''
  }

  user.profile = profile;

  return user;
});
  1. 为您的自定义配置文件公开已发布的字段
Meteor.publish('userProfile', function() {
  if(!this.userId) { 
    return null
  } else {
    return Meteor.users.find({_id: this.userId}, {
      fields: {
        username: 1,
        'profile.firstName': 1,
        'profile.birthday': 1,
        'profile.sex': 1,
        'profile.grade': 1,
        'profile.country': 1,
        'profile.hostel': 1,
        'profile.house': 1
      }
    });
  }
  1. 您需要查看的一般流星/JS 编码标准问题
  2. 在将其投入生产之前,您还需要检查其他事项,例如变量范围、单元测试、性能等。

作为一般规则(至少根据我的经验),我总是从最低限度开始,并避免在手头没有坚实的框架基础的情况下使用复杂的包。这将在将来为您节省大量的调试和挫败感:)。

请在您验证后接受此答案,以便我可以再次从我的 github 帐户中删除它。

祝你好运。

【讨论】:

  • 完美。绝对很棒。非常感谢! :) 这是我的第一个流星项目,我还需要学习很多东西。我肯定也会看看其他问题。我已经克隆了你的仓库,请随意删除它。
【解决方案2】:

请参阅下面我的工作示例代码供您参考:

用户详细信息模板标记

      {{#with currUser}}
        <form class="edit-todo" id="edit-user">
          Username:
          <input type="text" name="username" value="{{profile.name}}" />
          Firstname:
          <input type="text" name="firstname" value="{{profile.firstname}}" />
          Lastname:
          <input type="text" name="lastname" value="{{profile.lastname}}" />
          <button type="submit" style="width: 100px;height:50px">Save user</button>
        </form>
      {{/with}}

用户详情模板代码

Template.UsersDetail.events({
    'submit form#edit-user': function(e, tmpl) {
        e.preventDefault();

        var firstname = tmpl.find('input[name=firstname]').value;
        var lastname = tmpl.find('input[name=lastname]').value;
        var userId = Meteor.userId();

        Meteor.call('editName', userId, firstname, lastname, function(error) {
            if (error) {
                throw new Error(error.reason);
            } else {
                Router.go('users.detail', {
                    _id: userId
                });
            }
        });
    }
});
Template.UsersDetail.helpers({
    currUser: function() {
        return Meteor.users.findOne({
            _id: Meteor.userId()
        });
    }
});

名称更新的服务器方法

/*****************************************************************************/
/* Server Only Methods */

Meteor.methods({
    'editName': function(userId, firstName, lastName) {
        Meteor.users.update({_id: userId}, {$set: {
            'profile.firstname': firstName,
            'profile.lastname': lastName
        }});
        console.log(Meteor.users.findOne({_id: this.userId}).profile.lastname);// returns the proper name
    }
});

【讨论】:

  • 我希望这样做,但没有。一定是别的东西。更新函数一定是在推断。
  • 嗯...如果可能的话,请在公共 github 存储库或流星垫中提供您的项目(或 reproduc.test 项目),否则很难进一步调查。
  • 我会看看我能做什么,我对这一切(编码)很陌生。感谢您迄今为止的帮助!
  • 我设法将它上传到 github(我认为)。这是repo。如果您不介意,请看一下。
  • 好的,我明天检查一下
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-14
  • 2015-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多