简体   繁体   English

如何修复'类型错误无法读取未定义的属性0'?

[英]How to fix ' type error cannot read property 0 of undefined '?

I can't get row[].column_name in my .hbs file althougth when I console.log(row) it shows the result. 当我在console.log(row)显示结果时,我无法在我的.hbs文件中获取row[].column_name 。column_name。

The error is the following: 错误如下:

TypeError: Cannot read property 0 of undefined TypeError:无法读取undefined的属性0

router.get('/:id/baiviet', (req, res) => {
  var id = req.params.id;
  var p = baivietModel.allByCat(id);
  var q = chuyenmuc.singel(id); // singel(id) return a promise
  var row;

  q.then(rows => {
    row = rows;
  });

  p.then(rows => {
    for (const cm of res.locals.lcChuyenmuc) {
      if (cm.ID == +id) {
        cm.isActive = true;
      }
    }
    res.render('vwBaiviet/byCat', {
      baiviet: rows,
      CM: row[0]
    });
  }).catch(err => {
    console.log(err);
  });
});

My .hbs file: 我的.hbs文件:

{{this.CM.Name}}

TypeError: Cannot read property 0 of undefined TypeError:无法读取undefined的属性0

  var row;

Will initialize the row value to be undefined hence you're getting that error. 将初始化rowundefined因此您将收到该错误。

If you use Promise.all you can resolve two promises and send a value at once. 如果您使用Promise.all您可以解决两个承诺并立即发送一个值。

router.get('/:id/baiviet', (req, res) => {
  var {id} = req.params;

  Promise.all([
    baivietModel.allByCat(id),
    chuyenmuc.singel(id) // singel(id) return a promise
  ]).then(([prows, qrows]) => {

    // ... your other code

    res.render('vwBaiviet/byCat', {
      baiviet: prows,
      CM: qrows[0]
    });
  }).catch(err => {
    console.log(err);
  });

});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决此错误:无法读取未定义的属性“0” - how to fix this error: cannot read property '0' of undefined 如何修复类型错误无法读取天气预报项目中未定义的属性“cityName”? - How to fix Type Error cannot read property 'cityName' of undefined in the weather forecast project? 如何修复节点中的“无法读取未定义的属性'trim'”错误 - How to fix “Cannot read property 'trim' of undefined” error in node 如何修复 JavaScript 中的“无法读取未定义的属性‘匹配’”错误? - How to fix "Cannot read property 'match' of undefined" error in JavaScript? 如何修复NodeJS中的'TypeError:无法读取未定义的属性'标记'错误 - How to fix 'TypeError: Cannot read property 'tag' of undefined' error in NodeJS 如何修复NodeJS中的'无法读取未定义的属性'发射'错误 - How to fix ‘Cannot read property 'emit' of undefined’ error in NodeJS 如何修复“TypeError:无法读取未定义的属性 'id'”错误? - How to fix “TypeError: Cannot read property 'id' of undefined” error? 如何修复错误“无法读取未定义的属性‘执行’”? - How do I fix the error “Cannot read property 'execute' of undefined”? 如何修复类型错误:无法读取未定义的属性(读取“0”)? - How to fix the Type Error: Cannot read properties of undefined (reading '0')? 如何修复无法读取未定义的属性“hasOwnProperty”? - How to fix Cannot read property 'hasOwnProperty' of undefined?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM