[英]Meteor update user profile
I have no idea what's going wrong in my app. 我不知道我的应用程序出了什么问题。 I'm trying to update a user profile.
我正在尝试更新用户个人资料。 If a user has already a profile, it should display the current values of the profile.
如果用户已有配置文件,则应显示配置文件的当前值。 I have a SimpleSchema attached to the user collection.
我有一个附加到用户集合的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>
I have a template helper: 我有一个模板助手:
Template.updateCustomerProfile.events({
getUsers: function () {
//return Users.findOne();
return Meteor.user();
}
});
I have an Autoform hook 我有一个Autoform钩子
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()');
}
}
}
});
Have the following route: 有以下路线:
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'
});
}
});
and finally the following publication: 最后是以下出版物:
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});
}
});
And here is the collection: 这是集合:
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);
I'm sure the user object is passed in the publication. 我确定用户对象在发布中传递。 I can't update the profile: getting the following error (from Autoform debug):
我无法更新配置文件:收到以下错误(来自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"
How to go about updating a profile, staring blind.... 如何更新个人资料,盯着盲人....
You need to change your before AutoForm Hooks . 您需要在AutoForm Hooks之前更改您的。
AutoForm.addHooks(['profileForm'], {
before: {
insert: function(doc) {
console.log('doc: ', doc);
return doc;
},
update: function(doc) {
console.log('doc: ', doc);
return doc;
},
},
});
While the after
callback has the js standard (error, result)
function signature, the before
callback has just one parameter, the doc to insert/update. 虽然
after
回调具有js标准(error, result)
函数签名,但before
回调只有一个参数,即插入/更新的doc。 This is why you are always logging an 'error', it is just the doc you want to insert. 这就是您始终记录“错误”的原因,它只是您要插入的文档。 Also you need to either return it, or pass it to this.result to actually insert/update the object in the db.
您还需要返回它,或将其传递给this.result以实际插入/更新数据库中的对象。
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)
}
},
There are a couple small issues so I'm not sure how to tackle your problem, but here's some things to address. 有几个小问题,所以我不知道如何解决你的问题,但这里有一些事情需要解决。
Publish Method 发布方法
user
is never used. user
。 Were you trying to use it? userId
as a function parameter since you have access to this.userId
userId
包含为函数参数,因为您可以访问this.userId
Meteor.publish(null, ...)
so that it overrides the default current user publication Meteor.publish(null, ...)
以便它覆盖默认值当前用户出版物 Note: If you remove publish usersAllforCustomer
function, don't forget to remove it from route updateCustomerProfile
注意:如果删除发布
usersAllforCustomer
函数,请不要忘记将其从路由updateCustomerProfile
删除
Use Global Helper currentUser
使用Global Helper
currentUser
Here's how to update your template to use currentUser
instead of getUsers
以下是如何更新模板以使用
currentUser
而不是getUsers
<template name="updateCustomerProfile">
<div class="container">
<h1>Edit User</h1>
{{#with currentUser}}
{{#autoForm collection="Users" doc=this 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
{{/with}}
</div>
</template>
Hope this helps. 希望这可以帮助。
The meteorpad indeed solved the issue. 这个meteorpad确实解决了这个问题。 There was a mistake in the helper.
帮助者有一个错误。 In fact, the original code was:
事实上,原始代码是:
Template.updateCustomerProfile.events({
getUsers: function () {
return Meteor.user();
}
});
So in above snippet I was using an 'events' instead of an 'helper'. 所以在上面的代码片段中,我使用的是“事件”而不是“助手”。 Below is the correct code:
以下是正确的代码:
Template.updateCustomerProfile.helpers({
getUsers: function(){
return Meteor.user();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.