javascript - 将用户数据传递到 meteor 模板时无法读取未定义错误的属性

标签 javascript meteor

我不断收到以下错误:

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/

相关文章:

javascript - 谷歌地图标记删除的反向动画?

javascript - Meteor.call 'load' 参数

javascript - CollectionFS 访问被拒绝 403 错误 #Meteor JS

Angular 2 将表单分配给变量不起作用#f ="form"(提交)="onSubmit(f.value)"

javascript - 如何在 JavaScript 中检测滑动?

javascript - 允许移动用户滑动 slider

javascript - 使用自定义方法替换 native 对象(数组)方法 : Is it safe? 兼容吗?

javascript - Meteor react js登录用户邮箱

node.js - 运行mupx部署时引发新的NodeJS错误

javascript - 如何将动态样式属性设置为对象属性