【问题标题】:template rendered is not working properly in meteor JS渲染的模板在流星 JS 中无法正常工作
【发布时间】:2016-05-02 18:46:08
【问题描述】:

模板渲染不工作

当用户成功登录到系统时,我重定向到配置文件页面,该时间数据未获取,但如果我访问另一个页面并返回配置文件页面,该时间它工作正常。同样,当我重新加载页面时,它也无法正常工作

这里是代码

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('/');
    }
  }
});

【问题讨论】:

  • 两个流星调用,然后在第二个流星调用中循环?这将非常缓慢、异步且有问题。你想做什么?看起来您正在尝试为您的模板构建数据上下文,但是以一种非常非流星的方式。
  • @MichelFloyd 是对的。如果您需要从存储在 Mongo.Collection 中的服务器上传一些数据(如 cmets,在您的情况下),您应该使用发布/订阅 API。通常你会订阅 Template.profile.onCreated 或 waitOn 回调,以防你使用 iron:router 包。以 MDG 的待办事项列表为例:github.com/meteor/simple-todos.

标签: meteor


【解决方案1】:

当您刷新/重新加载页面时,会话值未定义。您可以使用meteor.user() 获取当前用户的电子邮件。你只需要像这样替换你的 session.get('email') 。

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

我希望这就是你要找的。​​p>

【讨论】:

  • 页面不刷新我只是使用“Router.route('/circles')”将登录页面重定向到个人资料页面。当页面重新加载时,我的会话邮件值未定义,因为我使用 Session.setPersistent。我的问题是登录后页面未正确呈现。
  • 使用“Router.go('/circles')”而不是“Router.route('/circles')”。
  • 查看我更新的代码,让我知道问题所在。谢谢
  • 我没有看到您在任何地方设置会话值。并且请不要使用过多的会话,这是流星的错误代码。
  • Session.setPersistent('first_name', result.first_name);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-16
  • 2015-08-26
  • 1970-01-01
  • 2014-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多