我不断收到以下错误:
Exception in template helper: TypeError: Cannot read property '_id' of undefined at Object.editable
我一直在尝试调试它,但没有成功。
我正在尝试将用户对象传递到 editUser 模板中。因此,当我单击“编辑”并获取表单时,我将能够在输入字段中看到用户以前的值,但无法让这些值显示在表单上。
这是我现在的代码:
# users.js
// Helpers
Template.user.helpers({
user: () => {
let user = Meteor.user() ? Meteor.user() : 'Unable to pull user info';
return user
},
editable: function () {
let currentUserId = Meteor.user()._id;
let currentUser = Meteor.user();
// let currentUserFind = Meteor.users.findOne({_id: this._id});
return Session.equals('editUser', currentUserId);
}
});
# user.tpl.jade
// Events
Template.user.events({
'click .form-edit'(e) {
e.preventDefault();
let currentUser= Meteor.user({});
let currentUserId = Meteor.user()._id;
let currentUserFind = Meteor.users.findOne({_id: this._id});
console.log(currentUserId);
console.log(currentUserFind);
// console.log(this._id);
Session.set('editUser', currentUserId);
// Session.set('editUser', this._id);
}
});
if editable
+editUser
else
with currentUser
.card.card--user
h3.card__title= username
p.card__content= profile.email
p.card__content= profile.firstName
p.card__content= profile.lastName
p.card__content= profile.bio
button.card__btn.form-edit Edit
我尝试传递 id
和用户对象,但无法让错误消失。
最佳答案
当您不导入 Meteor 伪全局时,我在 Meteor.user()._id
中看到了此异常。一旦您导入{Meteor}
,异常就会消失。 Meteor.userId()
也将完成这项工作。
关于javascript - 将用户数据传递到 meteor 模板时无法读取未定义错误的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39658141/