'Cannot read property '' of undefined' EJS 不渲染变量,尽管它已被定义
'Cannot read property '' of undefined' EJS doesn't render variable , allthough it is defined
这让我抓狂,请帮助我:
我将 node.js 的最新 v 与 express 、 mongo 和 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) 的结果是我所期望的:
正如我之前提到的,尽管有错误,但几乎所有的东西都在工作!!
尽管在获取具有相同错误的 /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............
问题是 docs
未定义。是什么让您认为它已定义?
这让我抓狂,请帮助我: 我将 node.js 的最新 v 与 express 、 mongo 和 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) 的结果是我所期望的:
正如我之前提到的,尽管有错误,但几乎所有的东西都在工作!! 尽管在获取具有相同错误的 /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............
问题是 docs
未定义。是什么让您认为它已定义?