2016-02-29 24 views
0

這使我瘋了,請幫助我: 我正在使用最新的node.js與express,mongo和EJS 的有趣的事實是網站仍然有效就好在控制檯下面的錯誤後:'無法讀取未定義的屬性'EJS不呈現變量,雖然它被定義爲

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)的結果是什麼,我希望:

res.json()

正如我前面提到的,的allmost一切工作儘管錯誤! 雖然得到/ 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............ 

回答

1

問題是docs沒有定義。你認爲它的定義是什麼?

+0

res.json(toSend)告訴我它已被定義? – user1311432

相關問題