猿问

无法访问javascript变量中的对象属性

我已经挣扎了一段时间寻求帮助。这真的很奇怪。我想访问一个对象属性,但这总是引发错误:


类型错误:无法读取未定义的属性模板


但我的应用程序运行正常。如果无法访问未定义的模板,则只有通知输出


//this is my object variabel 

var login = {};


login.data = {

    checkInput : formValidation,

    userSchema : User,

    template   : 'pages/users/login',

}



// so I add new method which I call in different files 

login.header = async(req, res, next) => { 

  /// in this section function I want to read property of template but it always return undefined 

  

  try {

    //  I have some code with read databases here 


    //some data i want to render 

    var data = {};

    res.render(this.data.template,data);

    

    // I've been also trying another way.

    var template = login.data.template !== undefined ?  'page/users/login' : login.data.template;

    

    res.render(login.data.template, data);

    

    // both of above always return output, but can't read template of undefined

    

  } catch(e) {

  

    throw new Error(e);

  }

}


拉莫斯之舞
浏览 239回答 2
2回答

茅侃侃

我一直在尝试使用没有数组函数的属性访问var login = {}login.data = {  template : 'admin/pages/'  body : {}};login.getViews = function(req, res, next) {  // it'l throw an error   res.render(this.data.template, this.data.body);     // than i try with another way, it works     res.render(login.data.template, login.data.body);     }
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答