gpt4 book ai didi

javascript - 如何在 Meteor 中编辑当前用户并向其添加新字段?

转载 作者:行者123 更新时间:2023-12-03 12:38:35 25 4
gpt4 key购买 nike

在 Meteor 中,当前用户只有一个“名称”字段。我想在个人资料下添加first_namelast_name

我的user_edit.html表单:

<template name="userEdit">
<div id="login-wrapper">
<form id="login-form" action="action" class="form login-form">
<div class="control-group">
<div class="controls">
<input name="first_name" type="text" value="" placeholder="First Name"/>
</div>
</div>
<div class="control-group">
<div class="controls">
<input name="last_name" type="text" value="" placeholder="Last Name"/>
</div>
</div>
<div class="control-group">
<div class="controls">
<input type="submit" value="Submit" id="submit-website-button" class="btn btn-primary"/>
</div>
</div>
</form>
</div>
</template>

我有一个用于编辑当前用户的表单:

我不太确定如何在 user_edit.js 中更新当前用户并为名字和姓氏添加其他字段。下面的代码不起作用。

Template.userEdit.events({

'submit form': function(e) {
e.preventDefault();

var currentUserId = this._id;

var userProperties = {
first_name: $(e.target).find('[name=first_name]').val(),
last_name: $(e.target).find('[name=last_name]').val()
}

User.update(currentUserId, {$set: userProperties}, function(error) {
if (error) {
// display the error to the user
alert(error.reason);
} else {
Router.go('userPage', {_id: currentUserId});
}
});

}

});

编辑

这是应用答案后我的 JS 文件的工作版本:

Template.userEdit.events({

'submit form': function(e) {
e.preventDefault();

Meteor.users.update( Meteor.userId(),
{
$set: {
'profile.first_name': $(e.target).find('[name=first_name]').val(),
'profile.last_name': $(e.target).find('[name=last_name]').val(),
'profile.cell_phone': $(e.target).find('[name=cell_phone]').val(),
'profile.email': $(e.target).find('[name=email]').val(),
'profile.business_name': $(e.target).find('[name=business_name]').val(),
'profile.website': $(e.target).find('[name=website]').val()
}
},

function(error) {
if (error) {
// display the error to the user
alert(error.reason);
} else {
Router.go('userPage', {_id: Meteor.userId() });
}
}
);
}
});

最佳答案

有几件事。我没有使用 Meteor 的过去几次更新,因此可能已添加“用户”作为光标来更新当前用户,但根据我的经验,这就是更新当前用户的方式:

Meteor.users.update( Meteor.userId(), { $set: { 'profile.first_name': "example" } } );

另请注意我如何指定“profile.first_name”,在您的代码片段中,当用户只允许编辑客户端的个人资料部分时,您将尝试在用户对象的基础中设置first_name和last_name。最后,我不确定模板上下文中的“this”是什么,因此我会设置 currentUserId = Meteor.userId(),这是获取当前用户 ID 的便捷方法。

关于javascript - 如何在 Meteor 中编辑当前用户并向其添加新字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23615997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com