流星collection2不更新

问题描述:

我在更新用户帐户时遇到了一些麻烦。我用下面的架构(collection2):流星collection2不更新

的lib /收藏/ users.js

Users = Meteor.users; 




var Schemas = {}; 


Schemas.User = new SimpleSchema({ 
gender: { 
    type: Number, 

    min: 1 

}, 

s_gender: { 
    type: Number, 
    min: 1, 
    optional:false 
}, 
picture: { 
    type: String, 
    custom: function() { 

     var base64Matcher = new RegExp("^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$"); 
     var value = this.value.replace("data:image/png;base64,",""); 
     if(!base64Matcher.test(value)) 
     { 
      return 'no picture'; 
     } 
     else 
     { 
      return true; 
     } 

    } 
} 
}); 

Users.attachSchema(Schemas.User); 

现在我用下面的代码更新:

客户端/模板/ start.js

Users.update({_id: Meteor.userId()}, { 
     $set: {picture: picture, gender: gender, s_gender: s_gender} 
    }, {validationContext: "updateUser"}, function (error, result) { 

     if (error) { 
      errorObjs = Users.simpleSchema().namedContext("updateUser").invalidKeys(); 

      console.log(errorObjs); 
     } 

     console.log(result); 
    }); 

验证通过,但结果中只显示“0”(错误为空) - 更新不起作用。如果我有一个空字段显示错误,所以验证工作正常。如果我分离架构,更新工作正常。

我在这里忘记了什么,或者为什么在验证通过时他不更新?

//编辑:另外我看到,Meteor不会再创建用户。

我相信你需要使用Users.profile.foo而不是Users.foo,因为Users是一个特殊的流星体,你只能在profile字段中保存新的字段。尝试使用简单模式/集合2建议用户模式作为起点(我将其复制)。

Schema = {}; 

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 
    }, 
    birthday: { 
     type: Date, 
     optional: true 
    }, 
    gender: { 
     type: String, 
     allowedValues: ['Male', 'Female'], 
     optional: true 
    }, 
    organization : { 
     type: String, 
     regEx: /^[a-z0-9A-z .]{3,30}$/, 
     optional: true 
    }, 
    website: { 
     type: String, 
     regEx: SimpleSchema.RegEx.Url, 
     optional: true 
    }, 
    bio: { 
     type: String, 
     optional: true 
    } 
}); 

Schema.User = new SimpleSchema({ 
    username: { 
     type: String, 
     regEx: /^[a-z0-9A-Z_]{3,15}$/ 
    }, 
    emails: { 
     type: [Object], 
     optional: true 
    }, 
    "emails.$.address": { 
     type: String, 
     regEx: SimpleSchema.RegEx.Email 
    }, 
    "emails.$.verified": { 
     type: Boolean 
    }, 
    createdAt: { 
     type: Date 
    }, 
    profile: { 
     type: Schema.UserProfile, 
     optional: true 
    }, 
    services: { 
     type: Object, 
     optional: true, 
     blackbox: true 
    } 
}); 

Meteor.users.attachSchema(Schema.User); 

来源:https://github.com/aldeed/meteor-collection2

的通知 “曲线模式” 是用户模式之前加载