将附加架构添加到用户集合后,Meteor 无法创建用户
Meteor not able to create user after adding attaching schema to users collection
我已经安装了 mizzao:user-status
包来跟踪用户 activity 喜欢在线、空闲状态。
我已将状态添加到用户集合:
import SimpleSchema from "simpl-schema";
const userSchema = new SimpleSchema({
status: {
type: Object,
optional: true,
},
"status.lastlogin": {
type: Object,
optional: true,
},
"status.lastlogin.date": {
type: Date,
optional: true,
},
"status.lastlogin.ipAddr": {
type: String,
optional: true,
},
"status.userAgent": {
type: String,
optional: true,
},
"status.idle": {
type: Boolean,
optional: true,
},
"status.lastActivity": {
type: Date,
optional: true,
},
"status.online": {
type: Boolean,
optional: true,
},
});
Meteor.users.attachSchema(userSchema);
在注册页面我有创建用户代码:
Accounts.createUser(
{ username, email, password },
async (error) => {
if (error && error.reason) {
setErrors({ signUpFailed: error.reason });
setIsLoading(false);
return;
}
navigate('/dashboard', { replace: true });
}
);
每当我尝试注册时,我都会在服务器上收到错误消息:
Exception while invoking method 'createUser' Error: After filtering out keys not in the schema, your object is now empty
状态中的所有字段都设置为可选,但我仍然收到此错误。如果我删除 Meteor.users.attachSchema(userSchema);
然后创建用户作品。
您可能需要描述
username: {
type: String
},
emails: {
type: Array,
optional: true
},
至少,可能还有其他领域,例如 profile
听起来您真正要找的是 https://docs.meteor.com/api/accounts-multi.html#AccountsServer-onCreateUser,这是您应该用来在创建用户文档时向其添加更多字段的回调函数。您实际上并不需要为此附加架构,从您的评论来看,架构的重点只是以某种方式扩展用户对象。如果我理解正确,那么 onCreateUser
绝对是正确的选择。
错误很明显。简而言之,它遍历了您的架构,删除了不在其中的内容,并发现它给您留下了空对象。因此,您需要定义用户集合中使用的所有其他字段。有两种方法可以避免此问题。首先是不要在用户集合上设置任何架构,其次是正确描述在那里设置和存储的所有内容。如果您正在调整用户文档,那么第二种方法是最好的。下面是我的描述,其中还包括 alanning:roles
的定义以供快速参考。您可以在 collection2 文档中找到更多详细信息:
https://github.com/Meteor-Community-Packages/meteor-collection2#attach-a-schema-to-meteorusers
const userSchema = 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.
optional: true
},
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.
optional: true
},
'emails.$': {
type: Object
},
'emails.$.address': {
type: String,
regEx: SimpleSchema.RegEx.Email,
optional: true
},
'emails.$.verified': {
type: Boolean,
optional: true
},
'emails.$.primary': {
type: Boolean,
optional: true
},
createdAt: {
type: Date
},
profile: {
type: Object,
optional: true,
blackbox: true
},
services: {
type: Object,
optional: true,
blackbox: true
},
roles: {
type: Array,
optional: true
},
'roles.$': {
type: Object,
optional: true,
blackbox: true
},
// In order to avoid an 'Exception in setInterval callback' from Meteor
heartbeat: {
type: Date,
optional: true
}
})
我已经安装了 mizzao:user-status
包来跟踪用户 activity 喜欢在线、空闲状态。
我已将状态添加到用户集合:
import SimpleSchema from "simpl-schema";
const userSchema = new SimpleSchema({
status: {
type: Object,
optional: true,
},
"status.lastlogin": {
type: Object,
optional: true,
},
"status.lastlogin.date": {
type: Date,
optional: true,
},
"status.lastlogin.ipAddr": {
type: String,
optional: true,
},
"status.userAgent": {
type: String,
optional: true,
},
"status.idle": {
type: Boolean,
optional: true,
},
"status.lastActivity": {
type: Date,
optional: true,
},
"status.online": {
type: Boolean,
optional: true,
},
});
Meteor.users.attachSchema(userSchema);
在注册页面我有创建用户代码:
Accounts.createUser(
{ username, email, password },
async (error) => {
if (error && error.reason) {
setErrors({ signUpFailed: error.reason });
setIsLoading(false);
return;
}
navigate('/dashboard', { replace: true });
}
);
每当我尝试注册时,我都会在服务器上收到错误消息:
Exception while invoking method 'createUser' Error: After filtering out keys not in the schema, your object is now empty
状态中的所有字段都设置为可选,但我仍然收到此错误。如果我删除 Meteor.users.attachSchema(userSchema);
然后创建用户作品。
您可能需要描述
username: {
type: String
},
emails: {
type: Array,
optional: true
},
至少,可能还有其他领域,例如 profile
听起来您真正要找的是 https://docs.meteor.com/api/accounts-multi.html#AccountsServer-onCreateUser,这是您应该用来在创建用户文档时向其添加更多字段的回调函数。您实际上并不需要为此附加架构,从您的评论来看,架构的重点只是以某种方式扩展用户对象。如果我理解正确,那么 onCreateUser
绝对是正确的选择。
错误很明显。简而言之,它遍历了您的架构,删除了不在其中的内容,并发现它给您留下了空对象。因此,您需要定义用户集合中使用的所有其他字段。有两种方法可以避免此问题。首先是不要在用户集合上设置任何架构,其次是正确描述在那里设置和存储的所有内容。如果您正在调整用户文档,那么第二种方法是最好的。下面是我的描述,其中还包括 alanning:roles
的定义以供快速参考。您可以在 collection2 文档中找到更多详细信息:
https://github.com/Meteor-Community-Packages/meteor-collection2#attach-a-schema-to-meteorusers
const userSchema = 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.
optional: true
},
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.
optional: true
},
'emails.$': {
type: Object
},
'emails.$.address': {
type: String,
regEx: SimpleSchema.RegEx.Email,
optional: true
},
'emails.$.verified': {
type: Boolean,
optional: true
},
'emails.$.primary': {
type: Boolean,
optional: true
},
createdAt: {
type: Date
},
profile: {
type: Object,
optional: true,
blackbox: true
},
services: {
type: Object,
optional: true,
blackbox: true
},
roles: {
type: Array,
optional: true
},
'roles.$': {
type: Object,
optional: true,
blackbox: true
},
// In order to avoid an 'Exception in setInterval callback' from Meteor
heartbeat: {
type: Date,
optional: true
}
})