template rendered is not working properly in meteor JS

江枫思渺然 提交于 2020-01-06 14:35:33

问题


template rendered is not working

when user successfully login in to system i redirect to profile page that time data is not get but if i visit another page and come back to profile page that time it is working fine. also when i reload page that time also it is not working

here is code

Template.profile.rendered = function(){
    
    var user_email  = {};
    user_email.mail = Session.get('email');
    
    var imgName  = Session.get('image');
    Meteor.call("imgSend",imgName,function(error, result){
        $('.user_profile_image').attr("src",result)
    });
    
    Meteor.call("getLinkMeta",user_email,function(error, result){
        
        var link_all_info = [];       
        var walldata      = [];
        var total         = result.length;
        var processed     = 0;
        
        var t =  result.forEach(function (entry){
			
              var link_info     = {};
              link_info.link_id = entry._id;                 

              Meteor.call("getCommentList",link_info, function (error, res){
				  
                if(error){
                    console.log("e");
                }else{
                    entry.comments = res;            
                }
                processed++
                if(processed == total){                                   
                   //walldata=result;
                }
             });                           
        });
        Template.profile.walldata = function(){  
                 return result;
        };    
			//return result;
  });
}

Router.route('profile', {
  path: '/profile',
  data: function() {
    
    /* Meteor.subscribe("Users");
       Meteor.subscribe("Link");
       Meteor.subscribe("Linkfav");
       Meteor.subscribe("LinkLike");
       Meteor.subscribe("LinkComment"); */ 
    
     $("body").removeClass('home');
     this.render('profile');
    
      setTimeout(function(){ 

        $('#username').html(Session.get('first_name'));
        $('#profile_username').html(Session.get('first_name'));
        $('#setting_name').val(Session.get('first_name'));
        $('#setting_username').val(Session.get('first_name'));
        $('#setting_email').val(Session.get('email'));
        $('#user_id').val(Session.get('id'));

        $('.setting_day').val(Session.get('day'));
        $('.setting_month').val(Session.get('month'));
        $('.setting_year').val(Session.get('year'));

        if(Session.get('image')!= ''){
          $('.user_profile_image').attr("src",Session.get('image'));
        }
        
        if(Session.get('gender') == 0){
            $('#user_gender').html('Male');
        }else{
            $('#user_gender').html('Female');
        }
            $('#day').html(Session.get('day'));
            $('#month').html(Session.get('month'));
            $('#year').html(Session.get('year'));
      },100);
    
  },onBeforeAction:function(){
    
    if(Session.get('email')){
        this.next();
    }else {
        //this.next();
        this.redirect('/');
    }
  }
});

回答1:


When you refresh/reload the page Session values are get undefined. You can get the current user email using meteor.user(). You just have to replace you session.get('email') like this.

var user_email  = {};
user_email.mail = Meteor.user().emails[0].address;

I hope that is what you are looking for.



来源:https://stackoverflow.com/questions/36951551/template-rendered-is-not-working-properly-in-meteor-js

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!