【问题标题】:How to make my own Meteor.users profile additions; using aldeed autoform and collection2如何添加我自己的 Meteor.users 个人资料;使用 aldeed autoform 和 collection2
【发布时间】:2015-11-23 06:24:14
【问题描述】:

我是流星和编码新手。我整天都在尝试使用 aldeed:autoform 和 aldeed:collection2 来向 Meteor.users 添加个人资料信息。我的成功各不相同,我几乎得到了我想要的(发布到 mongo,但创建了新的 id 而不是附加到当前的),但不知何故迷失了我的位置。现在,我不断得到

SimpleSchema invalid keys ... 0: Object
name: "emails"
type: "expectedArray"

我“提交”的任何内容都不会发布到 Mongo。

以下是我认为我需要的所有东西: 集合/simpleSchema.js

Meteor.users.allow({
    update: function (userId, doc){
        return !!userId;
    }
});


Schema.UserProfile = new SimpleSchema({
    firstName: {
        type: String,
        optional: true
    },
    lastName: {
        type: String,
        optional: true
    },
    birthday: {
        type: Date,
        optional: true
    },
    grade: {
        type: String,
        allowedValues: ['5', '6', '7', '8'],
        optional: true
    }
});

Schema.User = new SimpleSchema({        
    username: {
        type: String,
        // For accounts-password, either emails or username is required, but not both. It is OK to make this
        // optional here because the accounts-password package does its own validation.
        // Third-party login packages may not require either. Adjust this schema as necessary for your usage.
        optional: true,
        autoform: {
            type: "hidden"
        }
    },
    emails: {
        type: Array,
        // For accounts-password, either emails or username is required, but not both. It is OK to make this
        // optional here because the accounts-password package does its own validation.
        // Third-party login packages may not require either. Adjust this schema as necessary for your usage.
        optional: true,
        autoform: {
            type: "hidden"
        }
    },
    "emails.$": {
        type: Object,
        autoform: {
            type: "hidden"
        }
    },
    "emails.$.address": {
        type: String,
        regEx: SimpleSchema.RegEx.Email,
        autoform: {
            type: "hidden"
        }
    },
//    "emails.$.verified": {
//        type: Boolean
//    },
    createdAt: {
        type: Date,
        optional: true,
        autoValue: function(){
            return new Date();
        },
        autoform: {
            type: "hidden"
        }
    },
    profile: {
        type: Schema.UserProfile,
        optional: true
    },
    // Make sure this services field is in your schema if you're using any of the accounts packages
    services: {
        type: Object,
        optional: true,
        blackbox: true,
        autoform: {
            type: "hidden"
        }
    }
    // Add `roles` to your schema if you use the meteor-roles package.
    // Option 1: Object type
    // If you specify that type as Object, you must also specify the
    // `Roles.GLOBAL_GROUP` group whenever you add a user to a role.
    // Example:
    // Roles.addUsersToRoles(userId, ["admin"], Roles.GLOBAL_GROUP);
    // You can't mix and match adding with and without a group since
    // you will fail validation in some cases.
    //roles: {
    //    type: Object,
    //    optional: true,
    //    blackbox: true
    //}
    // Option 2: [String] type
    // If you are sure you will never need to use role groups, then
    // you can specify [String] as the type
//    roles: {
//        type: [String],
//        optional: true
//    }
});

Meteor.users.attachSchema(Schema.User);

client.js

SimpleSchema.debug = true

Template.NewUser.helpers({
  updateUserForm: function(){
    return Meteor.user;
  }
});

server.js

Meteor.methods({
 update: function(doc) {
   // Important server-side check for security and data integrity
   check(doc, Meteor.users);
    Meteor.users.clean(doc);
 }
});

感谢您的阅读!

【问题讨论】:

  • 这个问题还需要答案吗?您的 SimpleSchema 看起来是正确的。你能追踪错误发生在哪里吗?是在Meteor.users.clean(doc) 吗?也许添加一行额外的代码来打印文档? console.log(JSON.stringify(doc));看看是否有 emails 数组。或者根本没有 emails 属性。

标签: javascript mongodb meteor meteor-autoform meteor-collection2


【解决方案1】:

您没有在您的内容中包含您的 html。但是,您似乎正在使用自动表单上的方法更新。为了让它在你的服务器上工作,你需要调用 Meteor.user.update() 操作符。

updateProfile: function(doc, doc_id) {
  var theUser = Meteor.users.findOne({
    _id: doc_id
  });
  if (theUser._id !== Meteor.userId()) {
    throw new Meteor.Error("Not Authorised");
  } else {
    Meteor.users.update({
      _id: doc_id
    }, doc);
  }
},

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    相关资源
    最近更新 更多