【问题标题】:'Cannot read property '' of undefined' EJS doesn't render variable , allthough it is defined'无法读取未定义'EJS的属性''不呈现变量,尽管它已定义
【发布时间】:2016-06-12 00:13:15
【问题描述】:

这让我发疯了,请帮助我: 我正在使用带有 express、mongo 和 EJS 的 node.js 的最新 v 有趣的事实是,在控制台出现以下错误后,网站仍然可以正常工作

Cannot read property 'parent_category_id' of undefined
    at eval (eval at <anonymous> (E:\BITBUCKET repos\demo nodeJS\node_modules\ejs\lib\ejs.js:485:12), <anonymous>:20:27)
at returnedFn (E:\BITBUCKET repos\demo nodeJS\node_modules\ejs\lib\ejs.js:514:17)
at View.exports.renderFile [as engine] (E:\BITBUCKET repos\demo nodeJS\node_modules\ejs\lib\ejs.js:358:31)
at View.render (E:\BITBUCKET repos\demo nodeJS\node_modules\express\lib\view.js:126:8)
at tryRender (E:\BITBUCKET repos\demo nodeJS\node_modules\express\lib\application.js:639:10)
at EventEmitter.render (E:\BITBUCKET repos\demo nodeJS\node_modules\express\lib\application.js:591:3)
at ServerResponse.render (E:\BITBUCKET repos\demo nodeJS\node_modules\express\lib\response.js:961:7)
at E:\BITBUCKET repos\demo AndreiRadulescu nodeJS\Router.js:108:12
at handleCallback (E:\BITBUCKET repos\demo nodeJS\node_modules\mongodb\lib\utils.js:96:12)
at E:\BITBUCKET repos\demo 

nodeJS\node_modules\mongodb\lib\cursor.js:851:16

我的路由器:

router.get(
'/:categ/:subCateg' ,
function ( req , res )
{
    var subC  = req.params.subCateg;
    var categ = req.params.categ;
    req.app.locals.db.collection( "categories" )
       .find
       (
           { categories : { $elemMatch : { id : subC } } }
       )
       //gets only the categories we're interested in
       .map( function ( u ) { return u.categories[ 0 ] } )
       .toArray
       (
           function ( err , docs )
           {
               var toSend = docs[ 0 ];
               //res.json( toSend );
               res.render(
                   "subCategs" , {
                       _     : _ ,
                       docs : toSend ,
                       ...

res.json(toSend) 的结果是我所期望的:

正如我之前提到的,尽管出现了错误,但几乎一切正常! 但是,当获取 /api/ 时出现相同的错误,它会中断。

我怀疑我的其他路线可能有问题:

router.get(
    '/:root/:topC/:prods' , function ( req , res )...

router.get(
'/:root/:topC/:subC/:prodId' ,function ( req , res )...

router.get(
'/api/:SOAPmethod' , function ( req , res ) ...

GET /
GET /mens/mens-clothing
GET /mens/mens-clothing/mens-clothing-suits
GET /mens/mens-clothing/mens-clothing-suits/25604524
GET /api/getall
TypeError: E:\BITBUCKET repos\demo nodeJS\views\subCategs.ejs:8
    6|     <title><%=title%></title>
    7|     <style>
 >> 8|         #<%=docs["parent_category_id"]%>
    9|         {
   10|             background: rgba(178, 190, 255, 0.9)
   11|         ;

Cannot read property 'parent_category_id' of undefined............

【问题讨论】:

    标签: json node.js express undefined ejs


    【解决方案1】:

    问题是docs 没有定义。是什么让您认为它已定义?

    【讨论】:

    • res.json(toSend) 告诉我它已定义?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-22
    • 2021-03-11
    • 2020-05-31
    • 1970-01-01
    相关资源
    最近更新 更多